Re: checking if variable is defined
Re: checking if variable is defined
- Subject: Re: checking if variable is defined
- From: "Marc K. Myers" <email@hidden>
- Date: Sun, 21 Dec 2003 11:54:58 -0500
You can test for the existence of a variable by trying to set something
to it inside an "on error _ try" block. In this case I think it's
easier just to change the way the script is set up in order to avoid
the necessity of checking for it at all.
set fileID to (open file theFile with write permission
try
write someText to fileID
close fileID
on error
try
close fileID
on error
display dialog "File " & theFile & "would not close"
error number -128
end try
end try
Done this way an error in the open file statement would mean that the
file was not opened and the error would be captured at the script level.
Marc [12/21/03 11:54:06 AM]
Date: Sun, 21 Dec 2003 02:35:08 -0500
From: Graff <email@hidden>
Subject: checking if variable is defined
To: Graff <email@hidden>
Cc: John Fowler <email@hidden>, email@hidden
I just noticed a little problem with this, my test to see if the
variable "openedFile" is defined does not work. What is a proper way
to check to see if a variable is defined?
- Ken
On Dec 21, 2003, at 2:29 AM, Graff wrote:
You need to open the file with write permission. You should also
close it after you are done. It is also a good practice to put all
this in a try block so that you can make sure that no matter what
happens the file is closed:
-------------
set theFile to (path to desktop as text) & "mytextfile.txt"
try
set openedFile to open for access theFile with write permission
write "this text" to openedFile
close access openedFile
on error
if openedFile is not "" then
close access openedFile
end if
end try
-------------
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.