Applescript to do shell commands on save

78 views
Skip to first unread message

Jeroen van de Ven

unread,
Sep 3, 2014, 2:51:48 PM9/3/14
to bbe...@googlegroups.com
Hi,

I've been struggling to get my first script working, to do some stuff when a document is saved. This is what I have so far, and below it the error that arises. What's going on?

using terms from application "BBEdit"

on documentDidSave(docinfo)

set docpath to POSIX path of file of docinfo

do shell script "ls " & quoted form of docpath

end documentDidSave

end using terms from



Maarten Sneep

unread,
Sep 3, 2014, 5:24:04 PM9/3/14
to bbe...@googlegroups.com
If you make a run handler for the script, you can test everything in the script editor, and actually see which event returns the wrong result, and how to get the desired result.

I’ve split up the script, and because we are running from the script editor, we have to be a bit more explicit as to whom we’re talking to.

on docSave(docinfo)
tell application "BBEdit"
set docpath to POSIX path of file of docinfo
-- return file of docinfo
-- set docpath to (POSIX path of ((file of docinfo) as alias))
end tell
tell application "Terminal"
activate
do script "ls " & quoted form of docpath
end tell
end docSave

using terms from application "BBEdit"
on documentDidSave(docinfo)
docSave(docinfo)
end documentDidSave
end using terms from

on run
tell application "BBEdit" to set docinfo to document 1 of window 1
tell me to docSave(docinfo)
end run

-- End of script

When we run this, we see where things go wrong, and then suggestions what we need to inspect follow naturally: If we execute the first commented line we see that we get a file object. Apparently this does not know about POSIX paths (although the StandardAdditions dictionary says otherwise). However, if you turn the file object into an alias (second comment), then the POSIX path will do its magic.

Keep in mind that the do shell script command is limited, and that the output may not be available in an obvious way.

Maarten

Reply all
Reply to author
Forward
0 new messages