I’m completely new to Applescript (one week in) and I’m trying to complete the script below. I’ve outlined the goal of the script and used * where I’m stuck. Thank you for your help! I
--The goal of this script to copy contents of one folder into another. I'd like for the user to be asked to select the source and destination folders ONLY the very first time the script is run, and never again... so that each time the script is executed, the source and destination variables are already defined. This script is called as part of an VISE install build, and will be ran several times, so it's important that the user is only prompted once.
-- Why so complex? The company I work for does not want to have to use Finder hard coded paths to define variables (the want relative paths) yet they want the Applescript script to run silently when called from within VISE. So I figure, I could set relative paths the VERY FIRST TIME the script is ran, and then pass the data along.
--***Need code to test if the source and destintion were already selected by the user, and need to be able to reuse those variables through out the script.
-- This code allows the user to define the subinstaller source and destion folders
tell application "Finder"
set the source_folder to choose folder with prompt "Please select the source directory"
set the destination_folder to choose folder with prompt "Please select the destination directory"
set the source_path to POSIX path of the source_folder
end tell
-- ***I'm not sure how to complete this next if statement
if source_folder is destination_folder then display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
--This code copies the source data to the destination path using a combination of Applescript and UNIX ditto commands
if source_folder is not the destination_folder then
set the item_path to the quoted form of the POSIX path of source_path
set the destination_path to the quoted form of the POSIX path of destination_folder
do shell script ("/usr/bin/ditto -rsrc " & source_path & " " & destination_path)
end if
Model: MacBook
AppleScript: 1.10.6
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)
tell application "Finder"
repeat
set sourcePath to POSIX path of (choose folder with prompt "Please select the source directory")
set destinationPath to POSIX path of (choose folder with prompt "Please select the destination directory")
if sourcePath is destinationPath then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
exit repeat
end if
end repeat
end tell
NOTE: clicking “Cancel” on any of the dialogs will drop you out completely.
Hi Adam,
Thank you for the quick reply! That code is very useful and seems to work well.
However, there is another concern I wrote about in the original post, and perhaps there is another way of going about it: the Applescript will be executed “in the background” by another application (Mindvision VISE Installer). If the script prompts the user each time the script is executed, then it will no longer run silently.
Any ideas of how to prompt the user only once, store the source and destination variables within the script, and have the script self-utilize the variables, no longer prompting the user the next time the script is executed–an “ask me once and never again” method?
Thanks again,
risc
Hi, was working out one issue I saw with adams script.
is if the user Cancels, the sourcePath and destinationPath will still be set to the folder chosen.
This should negate that.
tell application "Finder"
try
repeat
set sourcePath to POSIX path of (choose folder with prompt "Please select the source directory")
set destinationPath to POSIX path of (choose folder with prompt "Please select the destination directory")
if sourcePath is destinationPath then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
exit repeat
end if
end repeat
on error error_message number error_number
if the error_number contains -128 then
set sourcePath to ""
set destinationPath to ""
else
end if
end try
end tell
in answer to you other question
I think if you set a check to see if the :
sourcePath
destinationPath
are equal to “” then
the script runs the prompt else gets on with the rest of its task.
property sourcePath : ""
property destinationPath : ""
on run
if sourcePath is not equal to "" then
my the_rest_of_the_script()
else
my ask_selection()
end if
end run
on ask_selection()
tell application "Finder"
try
repeat
set sourcePath to POSIX path of (choose folder with prompt "Please select the source directory")
set destinationPath to POSIX path of (choose folder with prompt "Please select the destination directory")
if sourcePath is destinationPath then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
exit repeat
end if
end repeat
my the_rest_of_the_script()
on error error_message number error_number
if the error_number contains -128 then
set sourcePath to ""
set destinationPath to ""
else
end if
end try
end tell
end ask_selection
on the_rest_of_the_script()
--- your other actions here
display dialog "this is an example of the rest of the script :paths set to" & return & sourcePath & return & destinationPath
end the_rest_of_the_script
edit** oops, forgot to add the call to the_rest_of_the_script() if the selection was completed correctly.
Hi Jacques and Mark,
Thank you both for your contributions! I modified the variable names at the end of Jacques’ script (removed the underscore), and things works great
Here’s another way to do it that is independent of whether it’s recompiled or not:
try
set source to do shell script "defaults read SrcDest 'Source'"
set dest to do shell script "defaults read SrcDest 'Dest'"
on error -- no such file
repeat
set sp to POSIX path of (choose folder with prompt "Please select the source directory")
set dp to POSIX path of (choose folder with prompt "Please select the destination directory")
if sp is dp then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
do shell script "defaults write SrcDest 'Source' " & sp
do shell script "defaults write SrcDest 'Dest' " & dp
exit repeat
end if
end repeat
end try
{source, dest} -- for use in the rest of your script
Of course to trash the settings, you have to trash ~/Library/Preferences/SrcDest.plist
The script needs to run more than once at different times ( like an application does) and store the chosen folders selection, but needs to only run this part of the script again (ever) if it has not been set.
property sourcePath : ""
property destinationPath : ""
on run
if sourcePath is not equal to "" then
my the_rest_of_the_script()
else
my ask_selection()
end if
end run
I would change that if line to:
if (sourcePath is not equal to “”) and (destinationPath is not equal to “”) then
That way you don’t have a chance that it will try to run even though the dest path was empty for some reason.
Model: Mac G5 OS 10.3.9
Browser: Safari 312.3.1
Operating System: Mac OS X (10.3.9)
Thank you for your help! I have a question regarding the “storing” of the placeholder {}
Are the {} values being stored into a plist? If so where? I cannot find a modified plist on my computer.
I think that may be a problem…
Say I do the following
Compile the script and save as “Move files”. Run the script and set the source and destination.
Make a copy of “Move files” in the Finder and name the script “Move Files 2”
Run the script “Move files”
Immediatly run the script “Move files 2” and choose a different source and destination than “Move Files”
Rerun the script “Move files” (OR create a copy of “Move files” title it “Move files 3” and run “Move files 3”
If “Move files” writes the {} to a plist and “Move files 2” overwrites the {} data to the SAME plist as Move Files, wouldn’t the paths of each copy of script “Move Files” overwrite the copy path from the last script?
Please help me to understand if I can run multiple instances of the script without losing the source and destination values for each version of the script.
Thanks!
I’m not sure I understand your question: “Are the {} values being stored into a plist?”. In a vanilla AppleScript the answer is no - when you declare set myVar to {} you are just defining the variable myVar as a list so you can fill it up later by adding to the beginning or end of it.
With respect to properties, I don’t think they’re preserved in a copy of a script.
With respect to the method in the previous post of storing to a pref file with defaults write, that will be there and if you copy the script, it will still be there so the new script will refer to the old pref file. To avoid that, you’d have to rename the pref file (in the old example, it’s SrcDest), and the easiest way to do this is to use the name of the application itself:
set P to path to me as alias
tell application "Finder" to set tApp to name of P
set SrcDst to quoted form of tApp -- Necessary to use quoted form if app. name is more than one word
--- Now use that name for the new app prefs which will be in Users/YourUserName/Library/Preferences
try
set source to do shell script "defaults read " & SrcDst & " 'Source'"
set dest to do shell script "defaults read " & SrcDst & "'Dest'"
on error -- no such entries
repeat
set sp to POSIX path of (choose folder with prompt "Please select the source directory")
set dp to POSIX path of (choose folder with prompt "Please select the destination directory")
if sp is dp then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
do shell script "defaults write " & SrcDst & " 'Source' " & sp
do shell script "defaults write " & SrcDst & " 'Dest' " & dp
exit repeat
end if
end repeat
end try
And from here on you just refer to the variables Source and Dest when you want a path to either.
if you save your script as a application. and run it it gets its own plist file automatically.
so this works in 10.4.7
set scriptname to displayed name of (info for (path to me)) as string
try
set source to do shell script "defaults read " & quoted form of scriptname & " 'Source' "
set dest to do shell script "defaults read " & quoted form of scriptname & " 'Dest' "
on error -- no such file
repeat
set sp to POSIX path of (choose folder with prompt "Please select the source directory")
set dp to POSIX path of (choose folder with prompt "Please select the destination directory")
if sp is dp then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
do shell script "defaults write " & quoted form of scriptname & " 'Source' " & sp
do shell script "defaults write " & quoted form of scriptname & " 'Dest' " & dp
exit repeat
end if
end repeat
end try
The new plist contains items like recent folders and other stuff
In lower than 10.4.7 I do not know if this is true so this should work if not.
set scriptname to displayed name of (info for (path to me)) & "_SrcDest" as string
try
set scriptname to name of me & "_SrcDest"
set source to do shell script "defaults read " & quoted form of scriptname & " 'Source'"
set dest to do shell script "defaults read " & quoted form of scriptname & " 'Dest'"
on error -- no such file
repeat
set sp to POSIX path of (choose folder with prompt "Please select the source directory")
set dp to POSIX path of (choose folder with prompt "Please select the destination directory")
if sp is dp then
display dialog "You cannot choose the same directory for both the source and the destination. Please try again."
else
do shell script "defaults write " & quoted form of scriptname & " 'Source' " & sp
do shell script "defaults write " & quoted form of scriptname & " 'Dest' " & dp
exit repeat
end if
end repeat
end try
Yes. When I started that last script, I was giving the plist file the same basename as the app (without the .app appended). Since I wanted to use the Property List Editor to check what had been written, I had the ~/Library/Preferences window open and saw that their were two new additions: “basename.plist” (mine), and “basename.app.plist” (system’s). No reason not to use what’s there, so I took the code for removing the “.app” extension out and ended up where you did.