Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

themacster298

macrumors member
Original poster
Feb 20, 2011
55
0
I am trying to make Preview open a picture, select all, copy, then quit.
I have no experience whatsoever.
Here is my code:
Code:
tell application "Finder"
	activate
	open document file "Chrome.icns" of folder "Other" of folder "iCons" of folder "Other" of folder "Documents" of folder "Mac" of folder "Users" of startup disk
		keystroke "a" using command down
		keystroke "c" using command down
		tell application "Preview"
		quit
	end tell
end tell
 
Last edited:

Partron22

macrumors 68030
Apr 13, 2011
2,655
808
Yes
That path construction is awkward. Look into path constants:
Code:
apple menu			preferences
application support		printer descriptions
applications folder		printer drivers
control panels			printmonitor
control strip modules		public folder
desktop				scripting additions
desktop pictures folder		scripts folder
documents folder		shared documents
extensions			shared libraries
favorites folder		shutdown folder
folder action scripts		sites folder
fonts				speakable items
frontmost application		startup disk
help				startup items
home folder			stationery
internet plugins		system folder
keychain folder			system preferences
launcher items folder		temporary items
library folder			trash
modem scripts			users folder
movies folder			utilities folder
music folder			voices
pictures folder
You'll soon be making paths that look like this
set filepath to (path to desktop as text) & "result:tubby.JPG"
--MUCH easier than what you've done.

Avoid nesting Tell statements when possible. You can do it in many situations, but it can also lead to complications unless you're sure what you're doing.


Try this:
Code:
--set filepath to (path to desktop as text) & "result:tubby.JPG"

tell application "Finder"
	activate
	--open file filepath	
	open file "Chrome.icns" of folder "Other" of folder "iCons" of folder "Other" of folder "Documents" of folder "Mac" of folder "Users" of startup disk
end tell

tell application "System Events" -- Syestem events handles keystrokes and mousedowns
	keystroke "a" using command down
	keystroke "c" using command down
end tell

tell application "Preview" to quit
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
As Partron22 as indicated, you need to set the path to your image file more
clearly, I have written a test script for you, but I have made it to work on
an image file called "Play Button.png" on my system, located in my Documents
folder, so you will have to point to an image file on your own system.

Code:
set picFile to (path to documents folder) & "Play Button.png" as text
try
	set picFile to picFile as alias
on error errMessage number errNumber
	display alert errMessage & space & "error number " & errNumber message ¬
		"Cant find file " & picFile & ", check file exists and the supplied path." as informational
	return
end try
tell application "Finder"
	try
		open document file picFile
	on error errMessage number errNumber
		display alert errMessage & space & "error number " & errNumber message ¬
			"Finder was unablke to open the file " & picFile as informational
		return
	end try
end tell
delay 0.5
tell application "System Events"
	set runningApps to name of every application process whose visible is equal to true
	if runningApps contains "Preview" then
		keystroke "a" using command down
		delay 0.5
		keystroke "c" using command down
		delay 0.5
		tell application "Preview" to quit
		return
	else
		display alert ¬
			"Could not select all, and copy to the clipboard the file" & picFile & return & ¬
			" because the application Preview is not active." as informational giving up after 30
		return
	end if
end tell

The First line of code sets the path to my image file as text, then the third line
changes the text into an alias type, which the Finder and System Events will
recorgnize as a file type.

You will notice that I have included some error checking into the script, this
is an important part of scripting and programming, as you dont want your
system to crash or display alert messages, when your file cant be found, or
the applications your sending commands too, does not understand these
commands.

Also I have put delays into the code, this is also important, as you need to
allow the Finder and Preview applications, time to react to your commands,
because if you send the commands to quickly, then the apps dont have time
to react, and you will get system error messages.

If you post the exact path to your image file, then I will show you how to
set a path to it, also you might consider using an open file dialog box, so that
you could choose any image file on youe computer, instead of hard coding the
path to only one image file.

Hope this helps you.

Regards Mark
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
In addition to what the others have posted, you also need to make sure you are selecting the correct item, since there can be other things in the window such as a table of contents or thumbnails. The script can vary depending on the layout, so one way to deal with that is to just choose the desired view. You don't mention what OS you are using, but in Lion it would be something like:

Code:
set appPath to POSIX file "/Applications/Preview.app" -- use Preview to open the file
tell application "Finder" to open (choose file) using appPath

delay 0.5 -- give Preview some time to open
tell application "System Events" to tell application process "Preview"
	click menu item "Content Only" of menu "View" of menu bar item "View" of menu bar 1 of it
	delay 0.5 -- give menu item time to perform
	keystroke "a" using {command down}
	delay 0.25
	keystroke "c" using {command down}
	delay 0.25
end tell

tell application "Preview" to quit
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.