Opinions on moving / deleting files

Hi everyone,

I have a script running at work that renames and uploads a bunch of files and then stores them on a server.

However, at times, using the Finder to communicate with the server seems to be a bit slow, and I’m looking at ways of speeding this process up (and also speeding the script up possibly).

So, say I want to delete a file, I could use:

set thefile to choose file
tell application "Finder"
	delete file thefile
end tell

But I’m wondering if a shell script might be better, e.g.:

set thefile to choose file
do shell script "rm " & (POSIX path of thefile)

or if using System Events might be better:

set thefile to choose file
tell application "System Events"
	delete disk item (POSIX path of thefile)
end tell

Does anyone have any opinions on what works better / quicker?

Hi,

the fastest and most reliable method is the second one, but use always the form quoted form of POSIX path.

Thanks very much for the reply – I will change my script over to that form then!

I have several Applescripts that I use several times daily to back up files to servers like MobileMe using the Finder. They are slow. I’d like to speed them up. I’m interested in learning more about the Unix scripts. Anyone know a good place to start? Also, can you use variables to set the path names in the Unix shell scripts? My current scripts all use variables to copy the files. Here’s an example of the actual line that does the copying in my current script.

 copy file (docName1 as string) to folder (destinationFolder) 

The variable destinationFolder is made up of the string “myMobileMeAccountName:Documents:” & the variable CurrentDayOfWeek & the variable TargetFolder.

Can you use variables like that in Unix shell commands? Any examples? Thanks.

Caper:

Everything you need to know about Unix shell scripting for the Mac can be found in this book. Although written for the Tiger OS, it is still current and relevant.

Good luck,

this line could never move or copy files, because the appropriate AppleScript commands are move and duplicate
Of course you can use variables in shell scripts. Read the man pages of mv, cp and ditto to get the options and proper syntax.

For example, this moves (not copies) a file to a folder. Unlike AppleScript, the mv command moves files even if the source and destination volumes are different


set theFile to choose file
set theFolder to choose folder
do shell script "/bin/mv " & quoted form of POSIX path of theFile & space & quoted form of POSIX path of theFolder

string paths and aliases can be coerced to POSIX paths, Finder file specifier can not

Thank you for your responses.

Stephan, I’m puzzled by the following:

My scripts work- and do copy the files, both to my backup hard disk, and to my mobileme disk. I used to use the move command, but someone told me to use ‘copy’ instead, can’t remember why. The line I quoted was the line from my script that I use- and it works. What am I missing? Should I go back to the move command?

if you use the Finder to copy or move files, the proper commands are duplicate and move, nothing else.
The copy command copies a value into a variable, similar to the set command.
But maybe you use a different application whose dictionary has a valid copy command to copy files

Stefan,

Thank you for your response. I will change the copy command to a move one. I very much appreciate your advice. Thank you for your patience with me.

In case you’re interested I will include the relevant code below. For some reason, my copy command does work in a Finder tell block. (This is not the full script, but I could post that if you like. The only thing I’ve changed is my real name to ‘myname’.)

tell application "Finder"
		mount volume "http://idisk.mac.com/myname/"
		copy docName as string to docName1
		set currentDay to weekday of (current date) as string
		
		if currentDay is not equal to day_script_last_run then
			set day_script_last_run to currentDay
			set targetFolder to 1
		end if
		
		copy "WD HD:WD Current Sermons:" & currentDay & ":" & targetFolder to destinationFolder
		try
			copy file (docName1 as string) to folder (destinationFolder)
		on error
			display dialog "Sorry, I was unable to move the file."
		end try
		copy "myname:Documents:Mobile Me Current Sermons:" & currentDay & ":" & targetFolder to destinationFolder
		try
			copy file (docName1 as string) to folder (destinationFolder)
		on error
			display dialog "Sorry, I was unable to move the file."
		end try
	end tell

Regards.