PDA

View Full Version : [Resolved] AppleScript help?




ArtOfWarfare
Aug 9, 2012, 11:31 AM
Hi, I tried typing my first script after looking at a few other ones, but it doesn't seem to be working... help would be appreciated.

Here's the script:
tell application "System Events"
tell application process "ClipEdit"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "New"
tell menu "New"
click menu item "Clipping from Clipboard"
end tell
end tell
end tell
end tell
end tell
end tell
end tell

delay 0.5

tell application "System Events"
tell application process "ClipEdit"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Copy"
end tell
end tell
end tell
end tell
end tell

delay 0.5

tell application "System Events"
tell application process "Preview"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New from Clipboard"
end tell
end tell
end tell
end tell
end tell

The error I get is this:
error "System Events got an error: Access for assistive devices is disabled." number -1719 from menu bar 1 of application process "ClipEdit"

But I've found that this error is often spat out at incorrect times (according to a google search for it.)

What I'm doing is I'm taking a selection of shapes from an iWork app and copying it. Ultimately, I'd like it if they were copied as bitmaps with alpha values, but if I try to open them directly in Preview, Preview will add a white background to them all and interpret them as a PDF, not a bitmap.

Thus I go through ClipEdit, which automatically interprets the shapes as bitmaps with alpha values. I open a new document from the clipboard in it, it automatically interprets it, I hit copy, and then I open it in Preview, which then shows what I want. I then apply a crop, rotate, and export as a png. (Too many variables in there, so I see no point in scripting that portion.)

It's a workflow I'm familiar with and have done for over a year, don't question it, I'm just trying to speed it up a little by using a script.

Thanks

Edit: I anticipate the possibilty of being asked about ClipEdit, so here's a link to the dev's page. He offers it as a free download. Someday here I aught to donate to him given how much I use ClipEdit...

http://www.everydaysoftware.net/clipedit/

Edit 2X: Whoops... turns out it really was an assistive device issue.



chown33
Aug 9, 2012, 11:52 AM
Edit 2X: Whoops... turns out it really was an assistive device issue.

I was going to suggest that you actually look and confirm that access for assistive devices was turned on. The debugging principle is this: check the most obvious things first.

Mark FX
Aug 9, 2012, 02:43 PM
You can check and change the setting for the access for assistive devices in System Preferences, from within your Applescript if you want to automate it, let me know if you do and I'll post some code for you.

Regards Mark

ArtOfWarfare
Aug 9, 2012, 11:56 PM
All I had to do to make this script work was add in a few activate application commands (otherwise it's a little confusing whether it worked or not) - and activate the assistive devices in System Preferences.

I have a few more questions regarding AppleScripts...

1 - How can I get it to close windows?
2 - Is there some way for me to make a script pause, then wait for the user to tell it to continue? (IE, it'd be nice if it automated naming/saving/moving the files from Preview after I've modified them the way I want... that part is all pretty systematic.)
3 - Is there some way for me to have it grab the window that it made most recently, and then resize it to fill the screen?

The script works well enough without these functions, but I can see my workflow going even quicker and smoother (and being less mundane and dull) with these improvements.

Mark FX
Aug 10, 2012, 01:24 PM
selecting a particular window of an application, and manipulating it is Application specific, that means that the particular application has to be scriptable, you can check to see if the application has a scripting definition file, or sdef dictionary file from the Applescript editor's, window library menu item.

for example the Finder Application has a limited number of methods for accessing its open windows.


tell application "Finder"
set openWindowList to get name of every window
return openWindowList
end tell


Gives an Applescript list with the title name of every open Finder window


tell application "Finder"
close window 1
end tell


This closes the frontmost window, as the frontmost window is numbered 1, and the other open windows, are increasing numbers as to there position.


Tell application "Finder"
close window named "Mark"
end tell


This close a Finder window with the name in the title bar of Mark, but will raise an error if no names window exists, but the first code snippet will give you the names of all open Finder windows, so check the name list first before trying to close it.

These are just a few of the type of commands that exist for the Finder application, but most of the Apple applications have the same or similar type of commands, like iTunes Preview ect.

So to awnser to your first question, check the particular applications scripting definition file for the properties and commands that can be accessed, alternatively you can do as you have been doing,
and script the GUI menu items for file close, or window zoom, he same as you've done in your original posting.

To pause a script you can use the standard delay command like this.


Tell application "Finder"
close window named "Mark"
delay 2.5
end tell


This will only pause the script from executing the next line of code for a certain period of set time, alternatively you could run a repeat loop until a certain condition has been reached, like this.


repeat while conditionIsTrue
delay 1
end repeat

--OR

repeat until conditionIsTrue
delay 1
end repeat

--OR

repeat while not booleanExpression
delay 1
end repeat


There is no actual pause command in Applescript as such, you have to create your own timers.

As for maximising a particular window, I would do the GUI scripting bit again, and click the window zoom menu item.

hope some of this helps.

Regards Mark

chown33
Aug 10, 2012, 03:14 PM
There is no actual pause command in Applescript as such, you have to create your own timers.

You can also display a dialog or alert, with a timeout. So it pauses with a dialog, whose OK/Cancel then continues, or it pauses until the timeout expires. Syntax is display THING giving up after NN_SECS. There are many more options, so open the scripting dictionary StandardAdditions.osax (AppleScript Editor > FIle > Open Dictionary... ).