A few fundamental Applescript questions

Hi all,

I’m new with Applescripting , just started yesterday to learn Applescript and I have to finish something in QuarkXpress that is urgent, I thought by posting my questions it could be accelerate my job for finishing this part of app. Anyway I got couple basic questions:

  1. How can I get an application path ?
    I tried this:
    copy (path to application of “MyApp”) to myApp_path

but it returns "Can’t get application MyApp that is running right now.

  1. How can we check for a specific file like ‘xyd.jpg’ whether it exits in desktop folder of startup disk
    2 -1) in case if exists, then copy the file to another path in the same disk?
    2-2) how we can get the full_path of ‘xyd.jpg’ after that file existed in desktop folder ?

I tried this:
tell application “Finder”
if (exists file “xyd.jpg” in desktop folder) then
move file “xyd.jpg” to folder “MyDestinationFolderName” of ¬
the startup disk with replacing

	end if
end tell

but it gives another error during runtime.

  1. And finally how we can pass a parameter to an Apple script when I call it from QuarkXpress application ?

TIA

Nima

Nima; I don’t have QuarkXpress, so don’t know whether it’s OS 9 or OS X. If, however, it’s OS X, then the reason you’re not getting any answers here is because this is the ‘9.2.2’ forum. All of OS X is in AppleScript | OS X.

The easiest way is this:

tell application "Finder" to set myAppPath to (path to applications folder as text) & "Chess"

What you had didn’t work because the “Finder” is in charge of paths. (That’s an oversimplification, but it will do for starters)

exists "full path to the file here"

is boolean - returns true or false. You use it in an if-then or if-then-else block.

Try this:


set myFile to alias ((path to desktop folder as text) & "applelogo.png") -- path to the file
set myDest to alias ((path to desktop folder as text) & "Moved:") -- path to the folder
if exists myFile then
	tell application "Finder" to move myFile to myDest
else
	display dialog "File not found"
end if

Make life a bit easier: go here and read some tutorials with examples

For that you need to be a bit more specific.

Hi, Nima.

  1. Leave out the of between application and “MyApp”. The of keyword generally means that one thing belongs to another. application “MyApp” is the specifier for a running application called “MyApp”.
copy (path to application "MyApp") to myApp_path
  1. When scripting the Finder, the Desktop folder is referred to simply as desktop. If you don’t specify where a file is, as in your move line, the Finder assumes you mean the desktop anyway. For clarity, it’s probably better either to mention the desktop both times or not to mention it either time.
tell application "Finder"
	if (exists file "xyd.jpg" in desktop) then
		move file "xyd.jpg" in desktop to folder "MyDestinationFolderName" of ¬
			the startup disk with replacing
		
	end if
end tell
  1. I’m not familiar with the workings of QuarkXpress or how it calls AppleScripts. The most probable way that the script would receive the parameters in the form of a list. The script would need an explicit run handler and a list of variables to receive the list of parameters:
-- Any properties defined here.

on run {a, b} -- This script expects to receive a two-item list when it's invoked.

	-- Write the "top level" script code in this explicit 'run' handler.

end run

-- Any other handlers used by the script go here or above the 'run' handler.

Note that the braces {} used after on run are not an alternative to the parentheses () used with normal handlers. run and open handlers receive just a single parameter (if any) which is a list of values. The braces used above are an AppleScript technique that assigns the values in a two-item list to the variables a and b.

I hope this helps. Good luck.