Senseless script to showcase my problem:
using terms from application "Mail"
on perform mail action with messages theMessages
set fullpath to (path to home folder as Unicode text) & ".local"
display dialog (POSIX path of fullpath) & "!"
end perform mail action with messages
end using terms from
When I save this script the part
set fullpath to (path to home folder as Unicode text) & ".local"
changes to
set fullpath to (path to home folder rule type Unicode text) & ".local"
This automatic change is fine at first but when I further work on the script again and save AppleScript Editor complains with
and has selected “rule type”.
If I change “rule type” to “,” it automatically changes that part to
set fullpath to {path to home folder, Unicode text} & ".local"
which is giving the right path anymore.
How do I solve this?
Edit: a quick idea I had seems to work correctly:
set fullpath to ((path to home folder) as Unicode text) & ".local"
Hi,
first of all, as Unicode text in
path to ... as Unicode text
is outdated since Leopard, where string, text and Unicode text have been merged to the same behavior (apart from the read/write command of Standard Additions).
Two suggestions:
Use a handler
using terms from application "Mail"
on perform mail action with messages theMessages
set fullpath to my getLocalFile()
display dialog (POSIX path of fullpath) & "!"
end perform mail action with messages
end using terms from
on getLocalFile()
return (path to home folder as text) & ".local"
end getLocalFile
Tell the AppleScript environment (current application) to do the job
using terms from application "Mail"
on perform mail action with messages theMessages
tell current application to set fullpath to (path to home folder as text) & ".local"
display dialog (POSIX path of fullpath) & "!"
end perform mail action with messages
end using terms from
Thanks for the answer. I need to experiment a bit more. For now I used the double (( )) to make it work.
My full script which looks at the mail subjects and runs the shell script etc is now:
(*
This script needs to be in ~/Library/Application Scripts/com.apple.mail/ that it is useable with newer OS X versions!
In Mail.app set a Rule
"if any of the following conditions are met:
Any recipient contains exult-cvs-logs@lists.sourceforge.net
Any recipient contains pentagram-cvs@lists.sourceforge.net
Any recipient contains nuvie-svn@lists.sourceforge.net
Any recipient contains xu4-commits@lists.sourceforge.net
Any recipient contains dosbox-cvs-log@lists.sourceforge.net
Run AppleScript snapshots"
The script will further check the subject for stuff
to make sure that it is new code and not other things
(e.g. Exult may have commits for the webspace repository)
*)
using terms from application "Mail"
on perform mail action with messages theMessages for rule Snapshots
tell application "Mail"
repeat with eachMessage in theMessages
set theSubject to the subject of eachMessage
set theBody to the content of eachMessage
set xu4 to "xU4"
set exult to "Exult"
set pent to "Pentagram"
set dos to "DOSBox"
set nuvie to "Nuvie"
if theSubject begins with "[" & xu4 and theSubject contains "trunk/u4" and theSubject does not contain "trunk/u4/src/iOS" then
set subj to xu4
else if theSubject begins with "[" & exult and theSubject contains "[exult/exult]" then
set subj to exult
else if theSubject begins with "[" & pent and theSubject contains pent & "/trunk" then
set subj to pent
else if theSubject begins with "[Dosbox" then
set subj to dos
else if theSubject begins with "[" & nuvie and theSubject contains "[nuvie/nuvie]" then
set subj to nuvie
end if
end repeat
end tell
# setting path to the folder of the lockfiles
set fullpath to ((path to home folder) as string) & ".local"
set lockfile1 to (fullpath & ":" & subj & "build1.lockfile")
set lockfile2 to (fullpath & ":" & subj & "build2.lockfile")
# For showing the icon in the Dialog, the icon path is set to the Apps' app folder in /Applications
set icon_path to ((path to applications folder) as string) & subj & ".app:Contents:Resources:" & subj & ".icns"
(*
Now we check for the two lockfiles and if necessary create them.
On first run of the script we create the first one (which eventually
will be deleted by the shell script we use to build the snapshot).
When the shell script is not yet done running but a new commit is being
detected by Mail.app this AppleScript is run again but will create a
second lockfile and will wait for (loop until) the first buildjob to
finish and delete the first lockfile.
In that case it will delete the second lockfile and recreate the first
one again.
IF there is another commit being detected while this script loops and
waits for the first lockfile to be deleted, the new AppleScript process will quit.
*)
tell application "Finder"
if exists file lockfile1 then
if exists file lockfile2 then
#display dialog (POSIX path of lockfile2) & " exists" # just for debug
return
else
make new file at fullpath with properties {name:subj & "build2.lockfile"}
repeat while (file lockfile1 exists) = true
end repeat
do shell script ("rm " & POSIX path of file lockfile2)
make new file at fullpath with properties {name:subj & "build1.lockfile"}
end if
else
make new file at fullpath with properties {name:subj & "build1.lockfile"}
if exists file lockfile2 then
do shell script ("rm " & POSIX path of file lockfile2)
end if
end if
end tell
activate
(*
Now the script asks you whether you want to build a new
Snapshot of either of those Projects.
Then it will open a new Terminal and execute the applicable
project snapshot script in ~/code/sh (where I store my scripts)
*)
set snapshotdialog to display dialog "New revision!!!
Build " & subj & " snapshot?
" buttons {"No", "OK"} giving up after 110 with icon alias icon_path with title subj
if button returned of snapshotdialog = "OK" or gave up of snapshotdialog is true then
tell application "Terminal"
activate
activate
tell application "System Events" to tell process "Terminal" to keystroke "t" using {command down}
delay 1
do script "cd ~/code/sh; . " & subj & "snapshot.sh" in selected tab of the front window
end tell
# if the build is canceled delete the first lockfile
else if button returned of snapshotdialog = "No" then
do shell script ("rm " & POSIX path of file lockfile1)
return
end if
end perform mail action with messages
end using terms from
Hi.
The explanation is probably that Mail uses the same token for ‘rule type’ as ‘path to’ does for ‘as’. Although compiled against ‘path to’, the token means ‘rule type’ in the context of Mail, so that’s how it appears on the screen and that’s what Mail thinks it means. There’s a similar conflict between AppleScript’s ‘text’ and Mail’s ‘rich text’.
Your fix using parentheses separates the ‘as’ from the ‘path to’, so the compiler doesn’t see ‘as’ as 'path to’s optional parameter but as an AppleScript coercion to be applied to the ‘path to’ result. This requires a different token and the confict doesn’t arise. However, parentheses won’t stop the ‘text’/‘rich text’ conflict.
Stefan’s suggestions take the scripting addition terminology right out of the Mail context, which is generally better practice anyway.
thanks for the explanation. Makes sense now and I’ll need to follow StefanK’s advice.