GUI automation with AppleScript - How to click a button in the script - macos

I'm trying to write an AppleScript that will use the Save as Pictures... function of PowerPoint, but I'm wrestling with AppleScript. This is what I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
delay 2
click menu item "Save as Pictures..." of menu bar item "File" of menu bar 1
end tell
and it isn't doing what I want. The specific error I get is:
script error: Expected end of line, etc. but found class name. (-2741)
And I don't know what to do. I've tried all sorts of things but I can't seem to get the right menu item to click. I'm using OSX 10.9 and PowerPoint 2011 (Version 14.3.2)
UPDATE:
This is now the script I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
And I'm getting an execution error: Microsoft PowerPoint got an error: Parameter error. (-50)

Gui automation is generally done through the "System Events" app.
Also you need to provide a full path to a file before trying to open it.
This is how you would begin your attack on PowerPoint correctly.
set p to POSIX file "/Users/drew/Desktop/file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell

Related

Applescript: Open preview "Export to pdf" error: Preview got an error: Can’t get keystroke " "

I would like to open all the pdfs in a folder with preview and export them to another folder using "Export to PDF".
This is my issue so far.
Error
Preview got an error: Can’t get keystroke "
".
Apple Script
tell application "Finder"
set fl to files of folder POSIX file "/Users/documents/PDFs" as alias list
end tell
repeat with f in fl
tell application "Preview"
open f
tell application "System Events" to tell process "Preview"
delay 0.2
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
end tell
keystroke return
end tell
end repeat
You are getting that error because keystroke return is a system events command and you have it within the "Preview tell block"
It looks as if your intention of keystroke return is to actually click the "Save" button, then this should work for you...
tell application "Preview"
activate
tell application "System Events" to tell process "Preview"
delay 0.2
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
delay 0.2
click button "Save" of sheet 1 of window 1
end tell
end tell

Applescript - Close Pages Document Automatically

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]

AppleScript Clicking On dialog box

In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?
If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell
The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.

Applescript running from AppleScript Editor not from the menu using BBEdit

I've read here around but I didn't find any fix for this stupid problem.
BBEdit, the most famous Mac text editor, should be widely scriptable and actually so it is. But,.. using Applescript I was trying to execute a menu command and there is no way at all. Or better, if I alternatively try:
tell application "System Events"
tell process "BBEdit"
tell menu bar 1
tell menu bar item "Markup"
tell menu "Markup"
tell menu item "CSS"
tell menu "CSS"
click menu item "Format"
end tell
end tell
end tell
end tell
end tell
end tell
end tell
Or also:
tell application "System Events" to keystroke "+" using {command down, shift down}
They works both running the Script from the Editor, but they don't work once I save the script and I choose it from BBEdit's AS Menu. Any idea ? Thanks.
Check how your script is saved: BBEdit's Script menu only runs compiled Applescripts (.scpt files), not text Applescripts (.applescript).
BTW, instead of the 7-way nested tell in the first snippet, just one nest suffices:
tell application "System Events"
tell process "BBEdit"'s menu bar 1's menu bar item "Markup"'s menu "Markup"'s ¬
menu item "CSS"'s menu "CSS" to click menu item "Format"
end tell

Script for adding new KeyboardShortcuts on MacOs (Leopard)

Is it possible to add KeyboardShortcuts in MacOs (Leopard) using shell or other programmatic way? Basically, something to automate the steps of opening Keyboard&Mouse in SystemPreferences, selecting the last tab "KeyboardShortcuts", Clicking "+" to add a new one and filling the info.
Thank you
The following AppleScript should do the trick, with 3 variables:
app_name: name of an application that you want to assign the shortcut to, e.g. Safari
menu_title: exact menu name to execute
keystrokes: the actual shortcut
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
tell window "Keyboard"
click button 3 of tab group 1
tell sheet 1
click pop up button 1
click last menu item of menu 1 of pop up button 1
keystroke "/Applications/" & app_name & ".app"
keystroke return
keystroke return
delay 1
keystroke menu_title
keystroke tab
keystroke last item of keystokes using rest of reverse of keystokes
delay 1
click button "Add"
end tell
end tell
end tell
end tell
The code is referencing the following site:
http://www.rngtng.com/2010/10/29/applescript-to-create-keyboard-shortcuts/

Resources