-1

我对编程很陌生,尤其是 AppleScript。我为情人节写了一个简单的脚本来播放 iTunes 中的歌曲,然后在 Safari 中打开一个 flash 动画文件。当我在 ScriptEditor 中运行脚本时,一切正常,但是当我作为独立应用程序导出时,它在启用全屏模式的命令中失败。我假设这是系统事件的问题。需要明确的是,应用程序运行到最后,但是在击键命令时我听到警报声并且窗口保持原样。

我正在运行优胜美地,并且已经完全更新。

理想情况下,我想在 Google Chrome 中打开文件以使用演示模式,但我什至无法让 Chrome 打开文件。

感谢您的任何建议!这是代码:

tell application "Finder"
  set visible of every process whose visible is true and name is not "Finder" to false
  close every window
end tell

set volume output volume 75

tell application "iTunes"
  set currentVolume to sound volume
    if player state is playing then
      stop
      back track
    end if
  play track "The Promise"
  set player position to 6
end tell

delay 4

tell application "Safari"
  activate
  if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
    make new window at front
  end if

open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
  tell application "System Events"
    tell process "Safari" to keystroke "f" using {command down, control down}

  end tell
end tell
4

2 回答 2

0

您很可能需要允许您的独立应用程序使用系统事件。在某些时候,您需要为 Script Editor 这样做;你需要为你的独立应用程序做同样的事情。

您将在“安全和隐私”下的“系统偏好设置”中找到该选项,然后是“隐私”和“辅助功能”。将会有一个应用列表,并且您的应用可能会在其中列出,而没有选中“允许以下应用控制您的计算机”。</p>

您可能需要使用“+”按钮将您的应用添加到列表中。

我已经验证我可以使用这个简单的脚本使 Safari 全屏显示;如果应用程序在可访问性下获得许可,它将起作用,如果没有,它将静默失败。

tell application "Safari"
    activate
end tell

tell application "System Events"
    tell process "Safari" to keystroke "f" using {command down, control down}
end tell

这是优胜美地,Mac OS X 10.10;在其他版本的 Mac OS X 中可能会有所不同。

于 2015-02-15T21:35:26.203 回答
0

我同意 Jerry Stratton 的评论,即这可能是一个可访问性问题。但是,也可能是您在 Safari 准备好接受它之前发出击键命令。如果它正在打开一个文件,那么它可能很忙并且错过了击键命令。

此外,我会将系统事件代码移到 Safari 代码之外,并且只告诉系统事件而不是 Safari 进程来执行击键命令。试试这个作为 Safari 和系统事件部分。

注意:我也无法让 Chrome 打开文件。

tell application "Safari"
    activate
    if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
        make new window at front
    end if

    open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell

tell application "Safari" to activate
delay 1
tell application "System Events"
    keystroke "f" using {command down, control down}
end tell
于 2015-02-15T23:33:59.277 回答