Getting an argument value

I’ve created an applescript program called VerifileExist. It’s purpose to read the filname passed as a parameter and to return a value as is the file exist or not.

I do have two problems:
1 - I do not know how to call the program VerifileExist and to pass the filename as an input argument;
2 - I am not sure if the program “VerifileExist” has the correct syntax

Thanks in advance for your help!

Daniel

Applescript program calling “VerifileExist”

tell application "VerifileExist"
display dialog "returned value"
end tell

APPLESCRIPT : VerifileExist

on run {input, parameters}
	set theFilename to ((path to home folder as text) & input)
        set valuetobereturned to "Does not exist"
	display dialog "theFilename: " & theFilename
	tell application "Finder"
		if exists file theFilename then
			display dialog theFilename & " exists!"
			set valuetobereturned to "File exists"
		end if
	end tell
	
	return valuetobereturned
end run

Hi

Here’s your script that you are trying to pass parameters to:


on run parameter_list -- a list of parameters
	set {theFilename} to parameter_list -- get the first parameter
	set theFilePath to ((path to home folder as text) & theFilename)
	set valuetobereturned to "Does not exist"
	tell application "System Events"
		if exists file theFilePath then
			display dialog theFilename & " exists!"
			set valuetobereturned to "File exists"
		end if
	end tell
	
	return valuetobereturned
end run

Here, the variable parameter_list contains only 1 parameter (the file’s name located in the home folder). Note that if you are displaying the file extension, then you must use the extension in the file name.

Call the application or compiled script with parameters:


set f to choose file -- choose the app or compiled script
run script f with parameters {"Hello"}

The name of my test file in the home folder, is “Hello.txt”. But I don’t have extension showing, so I can just use the displayed name “Hello”.

gl,

Model: MBP
AppleScript: 2.2.3
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Thanks gl,

The scripts are working, when the file exist the program is returning 0 as a value and returns -99 when the file does not exist.

I do have the following two questions :

  • What is the syntax to trap the value returned by the program? At this moment I can only see the value returned via the AppleScript Editor. I cannot find on the internet how to trap the messaged returned by the program.
set f to choose file -- choose the app or compiled script
run script f with parameters {"dl.txt"}
  • Is there a way to force the program to end and to return an error message I could trap with an “Try - On error - End try” solution ? I’ve tried it and cannot get it to work.
set f to choose file -- choose the app or compiled script
try
	run script f with parameters {"dll.txt"}
on error f number n
	if n = 0 then
		display dialog "The file exist"
	else if n = -99 then
		display dialog "The files does not exist"
	end if
end try

Many thanks for your help.

With regards!
Daniel

VerifileExist

 on run {input, parameters}
on run parameter_list -- a list of parameters
	set {theFilename} to parameter_list -- get the first parameter
	set theFilePath to ((path to home folder as text) & theFilename)
	set valuetobereturned to -99
	tell application "System Events"
		if exists file theFilePath then
			set valuetobereturned to 0
		end if
	end tell
	return valuetobereturned
end run

Hi,

the run handler is a normal handler which can return a return value.
So you can assign the result to a variable

Simple example


set theResult to addAtoB(2, 3)
theResult --> 5

on addAtoB(A, B)
	return A + B
end addAtoB

You can do the same with a script object

set f to choose file -- choose the app or compiled script
set theResult to run script f with parameters {"dl.txt"}
theResult --> "File exists" or "Does not exist"

For that purpose a boolean value is preferable, this example returns boolean true or false


on run parameter_list -- a list of parameters
	if parameter_list is {} then return false
	set theFilename to item 1 of parameter_list -- get the first parameter
	set theFilePath to ((path to home folder as text) & theFilename)
	tell application "System Events"
		return exists file theFilePath
	end tell
end run

Thanks Stefan,

By setting the result to a variable when running it does work.

In only have to perform an if statement to continue the rest of my work.

However, I also need to know if there is a way to force the program to end with an error which could be trapped by an on error condition ?

set f to choose file -- choose the app or compiled script
try
	run script f with parameters {"dll.txt"}
on error number n
	if n = 0 then
		display dialog "The file exist"
	else if n = -99 then
		display dialog "The files does not exist"
	end if
end try

I’ve tried using an “error” statement and the program does not end with an error to be trapped.

on run parameter_list -- a list of parameters
	set {theFilename} to parameter_list -- get the first parameter
	set theFilePath to ((path to home folder as text) & theFilename)
	set valuetobereturned to -99
	tell application "System Events"
		if not (exists file theFilePath) then
			error -99
		end if
	end tell
end run

with regards!
Daniel

why an error?

This is much simpler


on run parameter_list -- a list of parameters
	if parameter_list is {} then return false
	set theFilename to item 1 of parameter_list -- get the first parameter
	set theFilePath to ((path to home folder as text) & theFilename)
	tell application "System Events"
		return exists file theFilePath
	end tell
end run


set f to choose file -- choose the app or compiled script

set theResult to run script f with parameters {"dll.txt"}
if theResult is true then
	display dialog "The file exist"
else
	display dialog "The files does not exist"
end if

PS: If the home folder is always the target folder, you can even write


on run parameter_list -- a list of parameters
	if parameter_list is {} then return false
	set theFilename to item 1 of parameter_list -- get the first parameter
	tell application "System Events"
		return exists file theFilename of home folder
	end tell
end run

Thanks Stefan,

Why an error ?

There are situations where I will be using this program with other applications then AppleScript. I will then not be able to write a statement such as “set theResult to run…”

in those situations I will only be able to run the program and go further by seeing if the program ended normally or with an error.

For situation where I will be using AppleScript I will be using what you’ve suggested because it is way much simpler.

The folder will not always be the home folder I’ve used this as an example for me to get going. The final solution will be to have this working with external drives (for examples USB key, SDHC card, SAN…).

With regards!
Daniel

But the error is an AppleScript error and you can catch it only with an AppleScript try block.

An UNIX behavior to return a direct value and several return values via specific channels (stout, stdeer) is not possible with vanilla AppleScript

I see now.

Which programming language would you suggest I use to create such a functionality?

Thanks.

Daniel

any command line compatible language (e.g. C, Objective-C, Python, Ruby etc)

Every process has an stdin, stderr and stdout, it is assigned by the kernel. So when using osascript command line you have an difference between outputs and also return value. Personally I dislike try-catch methods.

How would you use this in other applications? If it’s through the command line I would use bash test command

[ -e /path/to/file ] && echo yes || echo no

This can be used in AppleScript and every programming languages that have ‘exec’ functions, in applescript with do shell script. This code should never error.

Hello.

First of all, the osascript with respect to standard output, error output, or return value, stumbles me a bit, as the return value of an osascript is returned through standard output. I also believe that an AppleScript error code is returned as the osascript’s exit code. (You can also redirect error messages to standard output with a command line option -so.)

Why do you dislke it? is it the operating on a separate stack, or is it the cluttering of the logic, or because a try error in combination, doesn’t really pinpoint what the error may be (context dependent)?

Just wondering, in most cases I do agree with you; better to handle the error in the do shell script, that may give you a better hint at where the error occured, and they can occur many places when a do shell script has more than one command in its string, so using try error on a do shell script, is a somewhat coarse measure.

Edit
In do shell scripts I avoid them, but inside osascripts, I really can’t have too many try - end try blocks. :slight_smile: