Im rather new to AppleScript, and have only written a few very timy scripts before. I am in the process of writing a little script that does 3 things…
Prompts the user for an imput text file
Runs a shell script wiith that file, and returns the output into a variable
Prompts the user to save the file with a standard “Save As…” dialog.
I have steps 1 & 2 working just fine…
tell application "Finder"
set input to choose file -- get the file
set FileName to name of input as text
end tell
set theOutput to do shell script "/Users/dczward/Desktop/cd2xml.pl " & FileName
return theOutput
but I’ll be damned if I can find the syntax for a “Save As…” dialog. Shouldn’t there be a saving equivalent of the “choose file” function? I can’t find it, and don’t know where to look. I scoured Google, the “dictionaries”, everywhere I can think of. I’m sure this is a super noobie question, so thanks in advance for pointing me in the right direction.
The commands for choosing a file or a new file name can be found in the Standard Additions scripting addition – no need to use the Finder and, in fact, your script will break if the file the user chose isn’t actually on the desktop. The easiest thing to do would be something like this (just pipe the results to the output file):
set input_file to quoted form of POSIX path of (choose file with prompt "Choose your input file:")
set output_file to quoted form of POSIX path of (choose file name default name "results.txt" with prompt "Save the results:")
set the_command to quoted form of POSIX path of ((path to desktop folder as Unicode text) & "cd2xml.pl")
do shell script the_command & " " & input_file & " > " & output_file
You could install the “cd2xml.pl” someplace and then reference the installed path (e.g., /usr/local/bin/cd2xml.pl) or you could create a script bundle and put it in the resources of that bundle and reference it with a “path to me” command (e.g., set the_command to quoted form of POSIX path of ((path to me as Unicode text) & “Contents:Resources:cd2xml.pl”).
everything works great per your suggestion, thanks. I can run the script by placing cd2xml.pl file into a “know” directory, (/usr/local/bin/scripts/cd2xml.pl). The only thing that I can’t seem to get working is the nice option of including the cd2xml.pl script into the .app as part of a bundle.
Here’s the steps I’m taking…
I open the “Bundle Contents” panel, create a new folder called “Resources”, and drag my script “cd2xml.pl” in there.
I change the "set the_command… " line to…
set the_command to quoted form of POSIX path of ((path to me as Unicode text) & "Contents:Resources:cd2xml.pl")
I choose File → Save As → File Format: application bundle →
Then, I test the script, and it chokes, saying it cant find the file “path:to:bundle.app:Contents:Resources:cd2xml.pl”. When I re-open the bundle.app in Script Editor, the Resources folder, and the cd2xml.pl file, are not there. It’s as if they aren’t getting saved.