Re: Error-trapping alias properties
Re: Error-trapping alias properties
- Subject: Re: Error-trapping alias properties
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 29 Mar 2002 21:53:32 -0800
On 3/29/02 3:56 AM, "Kai Edwards" <email@hidden> wrote:
>
> Can you store an alias as a script property, and still find a graceful way
>
> to set a new value (via choose file again, e.g.) if the file no longer
>
> exists?
>
>
>
> I know that storing the string file path to the file wouldn't hit this
>
> problem, but then it would throw up an error , or dialog if trapped, every
>
> time the file is moved or renamed. Is there some way around this?
>
>
There just may be, Paul.
>
>
I suspect the key to sneaking through the front door could be to initially
>
avoid a head-on 'confrontation' with the stored alias.
>
>
Instead, you may be able to use a reference to the alias, coerce that to a
>
string - and then perform a few text-based routines before testing for the
>
existence of the alias itself.
Paul,
This is it! It works, with suitable tests, both when the file is trashed,
and when moving the script to another computer. The peculiar thing, as Kai
discovered first, is that trashing the file results in the alias (and a
coercion to string as well) appearing as the initial path at when it was
first set (why is that, I wonder?). Even odder, it happens with an explicit
'get' as well:
property fileRef : ""
if fileRef = "" then
set theFile to choose file
set fileRef to a reference to theFile
display dialog "OK! All set up"
return
else
try
set theFile to contents of fileRef
get theFile
on error
display dialog "You trashed the file."
set theFile to choose file
set fileRef to a reference to theFile
end try
display dialog (theFile as string)
end if
---------
That does indeed get past the 'property block'. But it still gives the
initial path in the display dialog at the end, rather than erroring with
"You trashed the file".
However, as Emmanuel said, 'fully resolving' by FIRST coercing the alias to
string, then trying to coerce the string back to an alias, is what work:
try
set theFile to contents of fileRef
set filePath to theFile as string
get alias filePath
on error
display dialog "You trashed the file."
set theFile to choose file
set fileRef to a reference to theFile
end try
That, at last, results in "You trashed the file" both on the same computer
and also when moving the script to another computer, both in OS 10.1.3 and
9.2.2 (AS 1.8.2b3 on both).
Thanks to all who contributed. A successful conclusion.
--
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.