This is my first attempt to create an applescript.
What i'd like to do is create an apple script and then assing a custom shortcut to it so that it is launched when i need it.
I need it to quickly expand Thunderbird's insert -> HTML... menu.
My current applescript is nested into an automator workflow, and the code is the following:
on run {input, parameters}
tell application "Thunderbird"
activate
tell application "System Events"
tell process "Thunderbird"
click menu item 6 of menu 1 of menu bar item "Insert" of menu bar 1
end tell
end tell
end tell
return input
end run
And when i launch it (after grant permissions to automator), this is the error i get:
Syntax error: System Events got an error; Can't get menu bar item
"Insert" of menu bar 1 of process "Thunderbird"
I think that the proble is because the "Insert" menu is only available when the "Write new message" panel is open in thunderbird...
So my question is, how can i change the script above so that instead of only checking for "Thunderbird" app, it checks that both "thunderbird" and it's own "write new message" panel are open?
And also, is it liklely to be for this reason that i get this error, or i made some mistake in my applescript creation?
Thanks a lot and feel free to ask for more details.
Related
The problem in using the standard procedure (via System Preferences… > Keyboard) is that it needs the exact command name…
Many thanks in advance for any answer.
--
kenNash
I am not aware of any wildcard which could be used there, probably there is none.
But there is a silly workaround:
EDIT:
Also, this can be easily done in the Automator.
Create new Automator document like this:
Here is the code:
on run {input, parameters}
tell application "System Events" to tell process "Finder"
tell menu 1 of menu bar item 3 of menu bar 1
click (menu item 1 where name starts with "Rename")
end tell
end tell
return input
end run
It has to be saved to the ~/Library/Services/ directory.
Then set your keyboard shortcut for service:
You will also need to give accessibility permissions to finder:
I am trying to create a script that will automatically close the frontmost window of Apple Pages.
on run {}
tell application "System Events"
if (window 1 of process "Pages" exists) then
try
tell application "Pages"
--display dialog "Hello World!" --TODO: remove, test code only.
--Keywords I have tried: file, document, window,
close window 1 saving no
end tell
--close window 1 of process "Pages" saving no
on error errMsg
display dialog "ERROR: " & errMsg
end try
end if
end tell
end run
Whenever I run this, it gives me the following error:
ERROR: Pages got an error: window 1 doesn’t understand the “close”
message.
I have looked at this article, and have used the following command:
sudo defaults write /Applications/Pages.app/Contents/Info NSAppleScriptEnabled -bool YES
However, it still fails to work. Any advice?
Details:
System Version: OS X 10.9.1 (13B42)
Kernel Version: Darwin 13.0.0
Pages: 5.0.1
If Pages isn't scriptable, you're kind of out of luck. If it were scriptable, you wouldn't need System Events to close a window; that kind of functionality is usually included in a scriptable app's dictionary.
System Events can help with apps that aren't scriptable, but you have to rely on the actual UI. But if that's the solution, you can't use tell application "Pages" for the inner block (like you have it); you have to use:
tell process "Pages"
If you go that route, now you have to use either the close button on window 1 or use the close menu command. Something like:
activate application "Pages"--note that this will probably be NECESSARY (if it's not frontmost, it prob won't work)
tell application "System Events"
tell process "Pages"
click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1 of it
end tell
end tell
BUT then you have to come up with the problem of what happens if the window hasn't been saved (has been modified) -- which in a scriptable app uses the construction you were originally trying. When using System Events, you can do:
activate application "Pages"--note that this will probably be NECESSARY (if it's not frontmost, it prob won't work)
tell application "System Events"
tell process "Pages"
click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1 of it
delay .5
keystroke "d" using command down
end tell
end tell
But then again how do you make the script smart enough to know if the window has been modified or not? Or maybe you use System Events to see if the window has been killed after the close command, and if it hasn't, it does the keystroke thing. This kind of thing can be done by doing something like:
activate application "Pages"
tell application "System Events"
tell process "Pages"
set frontMostWinName to name of window 1
click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1 of it
tell me to delay 0.5
if exists window 1 then
if name of window 1 = frontMostWinName then keystroke "d" using command down
end if
end tell
end tell
I don't have Pages, but this works with another non-scriptable app, Bean (although I should mention that Bean uses tabs, and I had to move a tab to a window to get this to work*, and I don't know how Pages works in this regard).
[EDIT: *actually, this is not true; this works in Bean regardless of tabs/windows]
i was wondering how i could enable About MyApplication menu bar item on applescript. Ive tried this
tell application "System Events"
tell process "MyApp"
set AboutApplication to {}
repeat with AboutApplication in AboutApplication
try
set frontmost to true
set AboutApplication to enabled of menu item "About MyApp" of menu "Object" of menu bar item "Object" of menu bar 1
if activeMenuItem is true then
end if
end try
end repeat
end tell
end tell
but im pretty sure its wrong.
Can someone tell me how i could enable it?
I don't think you can do this with AppleScript.
If you are creating an AppleScriptObjC app, you need to add the appropriate script to the About menu item and use the XCode GUI to enable.
If you are writing a simple script using the Script Editor and want to enable a menu item in another app, it won't make sense, because that app doesn't have any code behind that menu item. Even if you were able to enable the menu item itself, nothing would happen if you clicked it, or a crash at best.
There is no sense in trying this.
I'm using Applescript to automate deployment of applications from Xcode 4.
I use System Events to click the menu items in the menu bar for that. I have the whole thing working, but with one quirk. If the user clicks elsewhere while my script is running, that is the XCode 4 window goes out of foreground, my entire script fails. Is there a way to force Xcode to be in the foreground from my script?
Now, if only Xcode 4 was at least as scriptable as Xcode 3, I wouldn't have to resort to GUI automation.
You can just use the activate command for every click call to ensure that the application is in the foreground. It's not ideal. Really if you're going to use System Events for scripting input like this you have to just accept that the user can't really use the computer whilst the script is running!
If you can break the script down into parts that require user input and parts that don't, you could present a dialog to the user saying something like, "Are you ready to continue with the script? You'll have to leave your computer for a while!"
... and then when it's finished, "Feel free to use your computer again now!"
This might make the script a little less obtrusive. Just a suggestion.
you should put up some of loading image or something while it is running also anytime you call a gui you should run a loop with a timeout that activates the app then checks for the existence of the items you want to click then when it exists click it and get out of the loop
EDIT
helpful link
A combination of mcgrailm and James Bedford's answers worked.
I put the menu click for "Edit Schemes…" in a loop until the edit scheme sheet becomes existent.
I also had to activate the application just before I clicked "Run without building".
The code:
tell application id "com.apple.dt.Xcode"
activate
end tell
tell application "System Events"
tell process "Xcode"
repeat until sheet 1 of window 2 exists
click menu item "Edit Scheme…" of menu "Product" of menu bar item "Product" of menu bar 1
tell application "Xcode"
activate
beep
end tell
end repeat
tell sheet 1 of window 2
set destination to pop up button 2 of group 1
click destination
set target to "iPad 4.3 Simulator"
click menu item target of destination's menu 1
set buildConfig to pop up button 3 of group 2
click buildConfig
click menu item "Debug" of menu 1 of buildConfig
click button "OK"
end tell
tell application "Xcode"
activate
beep
end tell
tell application id "com.apple.dt.Xcode"
activate
end tell
click menu item "Run Without Building" of menu 1 of menu item "Perform Action" of menu "Product" of menu bar item "Product" of menu bar 1
end tell
end tell
I am trying this simple GUI script to open a new window of Safari:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
try
tell menu bar 1
tell menu bar item 3
click menu item 1
end tell
end tell
on error theError
display dialog ("An error occurred while performing requested action" & theError) buttons "OK" default button "OK"
end try
end tell
end tell
but it is giving this error message:
Expected end of line but found """
Can anyone suggest me where I may be wrong?
Thanks,
Miraaj
Wow, that was weird. Your script broke AppleScript Editor. After running your script and it not working... I tried to recompile the script and then the error you posted starting showing up. So somehow your code caused AppleScript editor to break and thus the error. I had to quit and relaunch AppleScript Editor to get it working again.
I used the application UI Browser and found the problem. Your reference to the menu item was wrong. There's an extra menu in there that we can't see... and you didn't reference that extra menu. This is the problem with gui scripting. And even if a gui script works it may break at some future date as an application is updated. As such avoid gui scripting if at all possible.
Anyway, here's what your code should look like...
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
try
tell menu bar 1
tell menu bar item 3
click menu item 1 of menu 1
end tell
end tell
on error theError
display dialog ("An error occurred while performing requested action " & theError) buttons "OK" default button "OK"
end try
end tell
end tell
EDIT:
As I mentioned in my comment below, if you can't find a native command from an application's dictionary, the next most reliable method is using keyboard shortcuts. Most menu items have them. For example, if I wanted to open a new tab in a window that menu item has the keyboard shortcut command-t. So we can use that like this. Note there is a native command to open a new tab without using keystrokes, I'm just showing this as an example.
tell application "Safari" to activate
tell application "System Events"
keystroke "t" using command down
end tell
end
Keyboard commands don't usually change between application updates whereas gui commands often do because programmers redesign their interface in updates... and when that happens gui scripting goes haywire. One of the gotcha's with both gui scripting and keystrokes is that sometimes the script goes too fast and these techniques can't keep up with the speed of the program, so they often error. When this happens you need to slow down the script using small delays to allow the interface to keep up with the script.