AppleScript works in Script Editor but not as application - applescript

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

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

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.

Related

Applescript to open an application in full-screen mode?

I'm trying to program Alfred to open my Terminal, Sublime Text, and Chrome with a workflow.
I would like for my terminal to open normally as a window, but I've been trying to get Chrome and Sublime to open full screen.
I was able to get Chrome to open up in full screen mode with:
on alfred_script(q)
tell application "Google Chrome"
tell window 1 to enter presentation mode
end tell
end alfred_script
However, this did not translate to work with my Sublime Text.
What am I missing here?
Another way to do this assuming you have not changed the default keyboard shortcut for "Enter Full Screen" is simply to have System Events invoke that shortcut (⌃⌘F). As with the other approach I've seen to doing this (changing the value of AXFullScreen—see mklement0's answer here for a thorough discussion of this method), this requires making the relevant window active.
For instance, to toggle the full-screen state of the frontmost window in Safari, run:
tell application "Safari" to activate
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
As found here (i need an applescript to open safari in full screen an to hide the toolbar on mavericks). The make new document line prevents the can't get window 1 error by opening a new tab if one has not previously been opened.
tell application "Safari"
make new document
activate
delay 3
tell application "System Events" to tell process "Safari"
set value of attribute "AXFullScreen" of window 1 to true
end tell
end tell

script auto correcting incorrectly when i compile and key down key up not working as well

alright I'm currently having a lot of issues with my applescript editor. currently when ever i do a Key up key down sequence it will always key the letter A. for example.
tell application "System Events"
key down "u"
key up "u"
delay 1
end tell
this will keystroke A and i don't know why.
^ thats only a minor issue though my big problem is that i want to swap between three different video game applications. They are all a duplicate of the same game. Heres how my basic script goes.
tell application "Video Game" to activate
tell application "System Events"
keystroke a bunch of stuff
end tell
tell application "Video Game copy" to activate
tell application "System Events"
keystroke a bunch of stuff
end tell
tell application "Video Game copy 2" to activate
tell application "System Events"
keystroke a bunch of stuff
end tell
The problem is when i try to compile the script the application name always corrects to "Video Game".
Any ideas on how to keep it from compiling in correctly?
I have tried putting in a bogus name then when applescript asks me to find the app i click the correct one under the browse search box. and it will still compile incorrectly.
thanks a bunch :)
what you describe seems a bug in applescript. Here is a little workaround for that. I wrote it for jxa (javascript for ui automation), so you have to port it to applescript i think:
ObjC.import("Cocoa");
function key(aiKeyCode, aiDelay)
{
var source = $.CGEventSourceCreate($.kCGEventSourceStateCombinedSessionState),
keyDown = $.CGEventCreateKeyboardEvent(source, aiKeyCode, true),
keyUp = $.CGEventCreateKeyboardEvent(source, aiKeyCode, false);
$.CGEventPost($.kCGAnnotatedSessionEventTap, keyDown);
delay(aiDelay);
$.CGEventPost($.kCGAnnotatedSessionEventTap, keyUp);
}
keystroke will get the key to be pressed once. It helps to tell the process App you are telling System Events to do something, i.e.
tell application "System Events" to tell process "App Name" to keystroke "u"
If you want to do it with command, option, shift, etc. use this:
tell application "System Events" to tell process "App Name" to keystroke "u" using {command down, shift down, option down}
What AppleScript Editor is doing is not “autocorrect” — it’s “compiling.” It’s not doing it incorrectly. You can change the name of an app on the Mac and it doesn’t break the app. AppleScript Editor is still able to find the app “Video Game” even if you change its name to “Video Game copy 1.” This is a feature, not a bug.

Delay in an Alfred 2, using Automator and Apple Script to open "Stickies" and create a new note

Basically my goal is to code a key command (option-s) to activate Stickies and create a new note. Right now I have an Alfred 2 generated Automation which links the hot key to the following script:
on alfred_script(q)
tell application "Stickies" to activate
delay .2
tell application "Stickies" to activate
delay .01
tell application "System Events"
keystroke "n" using command down
end tell
end alfred_script
The two activate commands are my attempt to deal with a bug where it opens the application, but doesn't bring it to front. It works seamlessly when the application is open in the background, but it's slow and creates a screen flash when the application isn't already running. The delay is not coming from the application itself because I can open the application and hit command-n as fast as possible, and it always works.
(By the way if you have an idea for how I could hide all other notes and just show the new one, that would be awesome!)
Try this:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
set frontmost to true
end tell
If you run the script by pressing option-s, there might not be enough time to release option before keystroke "n" using command down.
Or this doesn't raise the windows for other notes:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
end tell
do shell script "open -a Stickies"
activate app "Appname" and set frontmost of "Appname" to true raise all windows, but do shell script "open -a Appname" raises only one window.
Hotkeys also have a short delay by default in Alfred, but you can reduce it by changing the trigger behavior:
You could try this alternate way, might have a different effect.
tell application "System Events"
tell process "Stickies"
set frontmost to true
keystroke "n" using command down
keystroke "Hello World" & linefeed & "I'm a new note!"
end tell
end tell
Hiding all other notes, i'd say start a new question for that.

AppleScript: How to reload a Safari window without bring the window to the foreground while I am in other Desktop space

I want to write an AppleScript to achieve a simple task. That is, I open a Safari window on Desktop 1 and automatically reload the browser every 30 minutes while I do my daily work on Desktop 2.
Here is my script:
tell application "Safari"
activate
tell application "System Events"
tell process "Safari"
keystroke "r" using {command down}
end tell
end tell
end tell
This script works. However, whenever the script is executed, my current Desktop 2 will be brought back to Desktop 1. It is distracting to my workflow.
Is there any way to just let Safari to reload in the background without bring Safari window to foreground on Desktop 1?
I have done a couple of searches; many of them say I should not use "activate" in my script. I tried that but then the script will just do nothing.
You can simply get the URL of document 1, then tell Safari to set the URL of document 1 to that URL.
The effect is the page will reload. Without bringing Safari to the front or jumping to Safari while you are in another space.
tell application "Safari"
set docUrl to URL of document 1
set URL of document 1 to docUrl
end tell
Also a simple on idle handler and saved as an Application (Stay open checked) that will run every 1 minute but not when Safari is fronmost. i.e when you are actually using it.
on idle
set frontApp to name of application (path to frontmost application as text)
if frontApp is not equal to "Safari" then
tell application "Safari"
set docUrl to URL of document 1
set URL of document 1 to docUrl
end tell
end if
return 60
end idle

Applescript; opening an app in Space number N

I wonder if it is possible in applescript to create a script for which we give as input the application name and a number N, so this app gets opened in the Space's space number N.
I would like with this to create a meta-applescript, so when the computer boots and after login, on each space I get different apps, and important, I can change this in the script file, and not through mac os x Space's preferences
Thanks
In OS X 10.5 or 10.6, Spaces assignments can be accessed and changed via the scriptable interface to System Events.app:
tell application "System Events"
set x to application bindings of spaces preferences of expose preferences
set x to {|com.apple.textedit|:4} & x -- Have TextEdit appear in space 4
set application bindings of spaces preferences of expose preferences to x
end tell
If you don't already know it, you can get the bundle id of an application from the Finder:
tell application "Finder"
get id of application file "TextEdit" of folder "Applications" of startup disk
end tell
This works to switch to Space 2 and then back to Space 1:
tell application "System Events"
key code 19 using {control down} -- control+2 is switch to Display Space 2
end tell
delay 1.0
tell application "System Events"
key code 18 using {control down} -- control+1 is switch to Display Space 1
end tell
delay 1.0
Answer
Although it's useful to assign applications to workspaces this doesn't address the question properly. Because, for example, you may want to launch multiple chrome windows in different spaces hence the application-to-space binding wouldn't work.
I found a workaround made of two steps to make this happen.
Change space location
tell application "System Events"
# comment: 18 to 21 somehow refer to workspaces 1 to 4, therefore here we are going to space number 1
tell application "System Events" to key code 18 using {control down}
end tell
delay 1 # comment: add some delay before launching app. this is 1 second delay
Launch the application that you want: either through another applescript or by using Launch Application
repeat the process to go to another space, and launch another app.
some notes:
Unfortunately, I have not found the corresponding key code to place an app on space number 5, if you do please let me know.
Also, this only works on the assumption that you already have the 4 spaces available (otherwise it will open things in the same space).
If things start to chain with previous scripts output and not work properly, remember to tick on each script/task of the automation Options > Ignore this action's input.
As personal observation once or twice when the computer seems busy to do something else, the prioritization of the automation seems low and the delay may be a few seconds longer.
Full example (with 4 apple scripts)
The following opens 2 google chrome windows in 2 different spaces. Code is below for copy and paste.
Script 1
tell application "System Events"
tell application "System Events" to key code 18 using {control down}
end tell
delay 1
Script 2
tell application "Google Chrome"
make new window
open location "https://www.google.com"
open location "https://www.apple.com"
end tell
delay 1
Script 3
tell application "System Events"
tell application "System Events" to key code 19 using {control down}
end tell
delay 1
Script 4
tell application "Google Chrome"
make new window
open location "https://www.bbc.co.uk"
end tell
delay 1

Resources