I have a short script that I started working on where I want Spotify to search for a specific song once an AppleScript is running, but I get this error that seems to be something that only Mavericks is whining about (after a short round of googling).
tell application "Spotify" to activate
delay 0.1
tell application "Spotify" to keystroke "l" using command down
It says
Syntax Error: Expected end of line, etc. but found identifier
and pointing to "using". Now, I've only been coding for a short period, but to my knowledge, this is the exact syntax that tons of people are using and have been posting to SO and other places.
Any ideas?
Cheers
The command keystroke belongs to the Processes Suite. You can call it with System Events.
tell application "Spotify" to activate
delay 0.1
tell application "System Events" to tell process "Spotify"
keystroke "l" using command down
end tell
Related
I want to get the foreground window title in macOS.
I have tried using AppleScript for this, it works but is very slow.
Here is the AppleScript code:
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
It takes a lot of time when we run this using Java.
Is there any other efficient solution to this?
I haven't used Java in ages, so I don't remember how it accesses frameworks (assuming I ever knew), but the framework you want is Quartz Window Services. These methods give you access to the window server, which manages all of the onscreen windows.
This AppleScript code works for me using the latest version of macOS Mojave.
For me, this AppleScript code is lightning fast.
try
tell application "System Events" to tell (process 1 whose it is frontmost) ¬
to tell (window 1 whose value of attribute "AXMain" is true) ¬
to set windowTitle to value of attribute "AXTitle"
end try
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.
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.
So I have a very basic Applescript here that basically hides all my social networking related apps, and then a similar one that unhides them. Problem is that if one of these apps isn't running then the entire thing halts at that line and won't set the ones below that line to hidden either.
Here's the script:
tell application "System Events" to set visible of process "Twitter" to false
tell application "System Events" to set visible of process "Wedge" to false
tell application "System Events" to set visible of process "Facebook" to false
tell application "System Events" to set visible of process "Adium" to false
tell application "System Events" to set visible of process "Messages" to false
Am I doing this the wrong way? Do I have to check to see if each one of these is running first, or is there some way to get it to continue with the script either way?
EDIT: Experiencing the same thing with this script, which I use with an Alfred hotkey to quit specific apps and start the screensaver when I leave work:
tell application "Adium" to quit
tell application "Messages" to quit
tell application "1Password" to quit
tell application "iTunes" to quit
tell application "ScreenSaverEngine" to activate
So basically if iTunes - for instance - isn't running then the screensaver will not start, but the 3 apps before that line will all quit.
You can wrap each statement in a try block so it will fail silently:
set myApps to {"iTunes", "1Password"}
repeat with anApp in myApps
try
tell application anApp to quit
end try
end repeat
You Could use the try command as specified above and use on error do nothing to do it.
Hint: with the [Enhanced Application Object Model][1] you can also do things like this:
if application "iTunes" is running then
tell application "iTunes" to quit
end if
or
tell application "iTunes"
if it is running then
pause
end if
end tell
[ How to check in AppleScript if an app is running, without launching it - via osascript utility ]
I'm trying to tell applescript to open the app, Application Loader, and choose from the drop down list that's in the window. The item that should be chosen will always be the very first item. How do i do this?
Thanks in advance.
Wow I finally got it working! I pretty much got mad at my script so I just put a random ASCII number and it magically worked. I got pretty happy aha :)
activate application "Application Loader"
tell application "System Events"
tell process "Application Loader"
tell the first combo box of window 1
delay 3
keystroke (ASCII character 30)
keystroke (ASCII character 12)
keystroke return
end tell
end tell
end tell