-1

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
nautilus
  • 3
  • 4

2 Answers2

0

Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.

You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”

You may need to use the “+” button to add your app to the list.

I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.

tell application "Safari"
    activate
end tell

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

This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
0

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
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Here is an especially nice way to see if an app is approved for UI Scripting: [AppleScript System Notifications Support (advanced)](http://macosxautomation.com/mavericks/notifications/01A.html). Other than that, maybe it is a problem if an app tries to make another app fullscreen? – McUsr Feb 16 '15 at 08:45
  • This solution worked for me. What I'm wondering is why does the script need the delay when run as an application, but not in Script Editor? – nautilus Feb 17 '15 at 19:37
  • It's difficult to say why. It all depends on what your computer activity level is at any given point. Whenever you gui script you should always use delays when testing and then when optimizing just continue to lower the delay times until they're as small as possible without causing errors. Glad this worked for you. Good luck. – regulus6633 Feb 17 '15 at 23:20