Applescript - Close Pages Document Automatically - applescript

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]

Related

With Applescript I need to use System Events to navigate a Finder Window that opens within an application

I'm currently writing some Applescript to import some material into software which doesn't have much applescript support and virtually no documentation on Applescript. The two methods are to somehow adapt this script to work:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
import media into document 1 from these_items
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to
However when I try to use "import media into document 1 from ________" I always get an error, in every incarnation of that combination.
My second approach then is using System Events to navigate through the menu bar to import everything. When I get to the import part of things where I can navigate and select the files, a finder window pops up. My question is:
How do I navigate this finder window within the application? This is like the inception of scripts. A finder window within a program.
I tried a few simple things like calling the front window of finder to navigate somewhere and what not. The current script that gets me to the import window is:
tell application "IsadoraCore"
activate
delay 2
tell application "System Events"
click menu bar item "File" of menu bar 1 of application process "IsadoraCore"
click menu item "Import Media..." of menu "File" of menu bar item "File" of menu bar 1 of application process "IsadoraCore"
end tell
end tell
Any help would be appreciated!
I would guess the "import media" window is a Finder window where you select a file to import. This window is easy to handle. To select the file you must have the posix path of the file. In that window if you keystroke "shift-command-g" then you get a text field where you can keystroke the posix path. For example, let's say I want to open a file in TextEdit. The file is on my desktop with the name test.txt. I can do this...
set filePath to (path to desktop as text) & "test.txt"
set posixPath to POSIX path of filePath
set aShortDelay to 0.5
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "o" using command down -- bring up the "open dialog window"
end tell
delay aShortDelay
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "g" using {shift down, command down} -- bring up the "goto folder sheet" in the open dialog window
delay aShortDelay
keystroke posixPath -- enter the posix file path
delay aShortDelay
keystroke return -- dismiss the "goto folder sheet"
delay aShortDelay
keystroke return -- dismiss the "open dialog window"
end tell
Notice I have delays after every command. That is good practice when you're using this type of scripting. You don't want the code to run faster than the computer can perform the tasks so the delay gives the computer time to keep up with the code. Notice that I use a variable for the delay. This way I can play with aShortDelay making it longer or shorter as needed to make the script run properly.
Good luck.
Aren’t you asking Isadora to import a list of folders? Perhaps it can only handle one folder at a time?
I don’t have Isadora to test, but you might try this:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
repeat with the_item in these_items
import media into document 1 from the_item
end repeat
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to

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

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

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 -> Activate window of a non-scriptable application

I have opened 2 "Finder" window A & B, A is in the front while B underneath, the following snippet brings B to the front the topmost:
tell application "Finder"
activate
activate window 2
end tell
But for applications that do not support scripting, the code just mentioned won't help.
Any ideas for activating a window of non-scripting application.
You can usually turn to system events in these cases. System events knows about the windows of running processes and you can usually manipulate those windows. Something like this will show you some of the things you can do. Just play around with the code and see if you can do what you want.
tell application "System Events"
tell process "Whatever"
properties of windows
end tell
end tell
EDIT: One of the properties of a window is its "title". So you might be able to use that. This approach uses the fact that many applications have a "Window" menu and under that menu many times the name of the windows are listed and you can switch windows by clicking the approprite menu item. So something like this might work... my example uses TextEdit.
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
set windowTitle to title of window 2
click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
end tell
end tell
What is your definition of non-scriptable? Just about everything is scriptable to some degree, but for the sake of an example lets use, does not contain an AppleScript dictionary, e.g. AppName.sdef within its application bundle.
For example, the macOS included Stickies application does not contain the Stickies.sdef file, and when trying to add it to the Library in Script Editor is says, "Unable to add the item because it is not scriptable."
In a case such as this, then System Events is needed to talk to the application process, e.g.:
Example AppleScript code:
if running of application "Stickies" then
tell application "System Events"
tell application process "Stickies"
set frontmost to true
if exists window 2 then ¬
perform action "AXRaise" of window 2
end tell
end tell
end if
Notes:
I've included error handling in the example AppleScript code, which can be removed if you prefer.

Simple GUI scripting question

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.

Resources