Search code examples
applescriptfullscreensystemevent

AppleScript works in Script Editor but not as application


I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.

I am running Yosemite, and am fully updated.

Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.

Thanks for any advice! Here is the code:

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

Solution

  • I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.

    Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.

    NOTE: I can't get Chrome to open a file either.

    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