Re: checking if variable is defined
Re: checking if variable is defined
- Subject: Re: checking if variable is defined
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 21 Dec 2003 01:26:48 -0800
Sorry, I misunderstood what you wanted. (Although you may want to look at
that script anyway...) To check whether a variable is defined, just use a
try block:
try
get openedFile
on error
set openedFile to open for access file theFilePath with write permission
end try
Don't be surprised: openedFile will just be a number. I think you might find
the script I sent last more useful, unless I've misunderstood also what you
want this for.
On 12/21/03 1:14 AM, "Paul Berkowitz" <email@hidden> wrote:
>
On 12/20/03 11:35 PM, "Graff" <email@hidden> wrote:
>
>
> 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
>
>> -------------
>
>
Do it the other way around:
>
>
-------------
>
set theFilePath to (path to desktop as Unicode text) & "mytextfile.txt"
>
--text, not a file
>
try
>
close access alias theFilePath -- only if exists and is open
>
end try
>
try
>
set fileRef to open for access file theFilePath with write permission
>
-- a number; note 'file'
>
write "this text" to fileRef
>
close access fileRef
>
on error
>
try
>
close access fileRef
>
end try
>
end try
--
Paul Berkowitz
_______________________________________________
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.