Re: Using a variable in a property
Re: Using a variable in a property
- Subject: Re: Using a variable in a property
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 11 Dec 2001 17:38:50 -0800
On 12/11/01 5:00 PM, "William Barnes" <email@hidden> wrote:
>
tell application "Finder"
>
activate
>
set filnam to characters 26 thru 35 of subjectvar
>
set destfol to "Macintosh HD:E-mail:AppleScript list:"
>
make file at destfol with properties ,
>
{name:filnam as string, creator type:"NISI", file type:"TEXT"}
>
set filRef to (open for access file filnam with write permission)
>
write bodyvar to filRef
>
save fileRef
>
close access filRef
>
end tell
>
>
The script chokes on the "make file..." line with an error message
>
"Invalid key form". I am assuming that this results from my trying to use
>
the variable 'filnam' for the 'name' property, and it cannot be coerced
>
into a string. Any suggestions would be greatly appreciated.
No that's not the problem 'characters' (a list) can be coerced to string. .
But you've defined destfol as a string when it needs to be an alias;
set destfol to alias "Macintosh HD:E-mail:AppleScript list:"
The Finder can't make a file at a string.
'characters 26 thru 35 of subjectvar' is not a string but a list. it _can_
be coerced, but should do it explicitly, after first making sure that the
text item delimiters are correctly at their default of "" so as not to
introduce other characters (a different delimiter) between each character.
I'm not certain if the Finder likes you doing this inside its 'make'
command, however. Or perhaps you might need to add "new", as in 'make new
file', which is the usual way of doing it. So you _could_ do outside the
Finder block:
set AppleScripts text item delimiters to {""}
set filnam to (filnam as string)
But there's much better way, which is also much quicker, and doesn't need to
bother accessing every single character separately:
set filnam to text 26 thru 35 of subjectvar -- that's a string
Then 'make new'. Then you'll hit another error: 'file filnam' does not give
an adequate definition of the file to the Finder. it either needs its own
longwinded terms'
file filnam of folder "AppleScript list" of folder "E-mail" of disk
"Macintosh HD"
or , simpler:
alias (destfol & filnam)
or else set a variable when making the file:
set theFile to make new file with properties ...
then
set theFile to theFile as alias
Then, you don't need anything except making the file to be in the Finder
block - it slows things down at best. And there's also no good reason to
activate the Finder (nor Emailer either). And it's better to reduce the
number of AppleEvents: just get the message once, not twice, and set a
variable to it. Finally, it's the content of the message (the body) , not
the actual message object, which I think you want to write to the text file.
tell application "Claris Emailer"
set theMsg to message 1 of folder "AppleScript list"
set bodyvar to content of theMsg
set subjectvar to subject of theMsg
end tell
set filnam to text 26 thru 35 of subjectvar
set destfol to alias "Macintosh HD:E-mail:AppleScript list:"
tell application "Finder" to set theFile to make new file at alias destfol
with properties {name:filnam, creator type:"NISI", file type:"TEXT"} -- one
line
set theFile to (theFile as alias)
set filRef to (open for access theFile with write permission)
write bodyvar to filRef
-- cut out saving outside the Finder, and you had a typing error
close access filRef
--
Paul Berkowitz