Folder action to change file ext, move file & copy file

I’m a scripting noobie (I cut-n-paste some but no real coding)… I’d realy apreciate some help:

I download a lecture every day to listen to on my palm. Sometimes the file comes in w/ a “.mpga” extension. iTunes recognizes it ok, but PocketTunes (on the palm) doesn’t – unless I change the ext to “.mp3”.

So, every day I download the file, if necessary manually change the name (w/ a dialog, “are you sure…”), manually move it to a different folder, and then copy it to an SD card (mounted on the desktop).

I figure this is a perfect candidate for a folder action, but I don’t know where to begin…

  • All the file names start the same: “SVT”

  • If the file has an “mpga” ext, switch it to “mp3” w/o a dialog

  • move the file to the “SVT” folder on my HD

  • check to see that the volume “Palm SD” is mounted (put up a dialog if not, “please mount blah blah blah and hit ok”)

  • copy the file to the SVT folder on “Palm SD”

Any takers?

G4, OS X (10.3.9)

TIA,
wogo

OK, I have no idea if this is good code or not but it’s starting to work… But I still need help…

Here’s what I have so far:

on adding folder items to this_folder after receiving added_items
tell application “Finder”
repeat with aFile in added_items

		if the name of aFile contains "SVT" then

– This next part doesn’t work (if the file ends in “mp3” it moves & copies it. if the file ends in “mpga” it does nothing)
if the name extension of aFile is “mpga” then
set the name extension of aFile to “mp3”
end if

– this bit works fine
move file aFile to “Warp Drive:Other Stuff:SVT”

– This also doesn’t work:
– I wanted some kind of check if the drive is mounted and a dialog if it’s not.
– “exists” means that it is mounted, but “doesn’t exist” is apparently bad syntax
if disk “PALM_SD” doesn’t exist then
display dialog ¬
“Please mount the SD card”
end if

–The copying also works ok
try
copy file aFile to folder “PALM_SD:Audio”
end try

		end if
	end repeat
end tell

end adding folder items to

Anybody???

-Wogo

wogo,

Try this for the first part.

set t to choose file
set astid to AppleScript's text item delimiters
tell application "Finder"
	if name of t contains "SVT" and name extension of t is "mpga" then
		set AppleScript's text item delimiters to "."
		set tName to name of t
		set fTName to first text item of tName -- this separates the extension from the first part of the name. If your file name has more than one period in it then you may have to add code to get the part of the name you need.
		display dialog fTName -- this was just a check for the result.
		set properties of t to {name:fTName & ".mp3"}
	end if
end tell
set AppleScript's text item delimiters to astid

On the mounting of the disk you need to put the code into a Finder tell block:

tell application "Finder"
	if not (exists disk "PALM_SD") then
		display dialog "Disk not present. Please mount disk."
	end if
end tell

PreTech

Thanks!!!

I actually ended up figuring some of this stuff out on my own (mostly by trying to decipher other scripts) but there is no way I would have figured out the “mpga” → “mp3” switch.

The temp solution that I came up with was:

if the name of aFile does not end with ".mp3" then
	set the name of aFile to name of aFile & ".mp3"
end if 

which kept the “.mpga” but added “.mp3” (so the file was called “SVT001.mpga.mp3”) - not an elegant solution, but it worked… But your’s is better so I used it (even though I have no idea how it works)…

How do I get it to replace same-named files? Or even better - to rename them? At the moment, if a file of that name exists it just stops the script cold.

Here is the final folder action:

on adding folder items to this_folder after receiving added_items
	
	set astid to AppleScript's text item delimiters --don't know what this does
	
	tell application "Finder"
		repeat with aFile in added_items
			if the name of aFile contains "SVT" then
				
				--changes .mpga to .mp3 (don't know how)
				if name extension of aFile is "mpga" then
					set AppleScript's text item delimiters to "."
					set tName to name of aFile
					set fTName to first text item of tName -- this separates the extension from the first part of the name. If your file name has more than one period in it then you may have to add code to get the part of the name you need.
					set properties of aFile to {name:fTName & ".mp3"}
				end if
				
				--only processes .mp3 files (in case a different type of file contains "SVT")
				if name extension of aFile is "mp3" then
					move file aFile to "Warp Drive:Other Stuff:SVT"
					
					--make sure card is mounted
					if (exists of disk "PALMSD") is false then
						display dialog "Please mount the SD card"
					end if
					
					copy file aFile to folder "PALMSD:Audio:SVT"
				end if
			end if
			
		end repeat
	end tell
	
	set AppleScript's text item delimiters to astid --don't know what this does
	
end adding folder items to

I don’t have time at the moment to write out exactly how to do that in your script, but I do have a piece of code that I used on one of my scripts to do what you are looking for. It’s not super-fancy. It appends a “_2” to the files name if the file already exists. If you run it a second time on the same file you end up with “_2_2” and so on. Maybe someone out there is clever enough to alter it to count “_2”, “_3”, etc. Anyway, here it is and I hope you can adapt it to work in your script:



set PathToDesktop to path to the desktop as text  --I use this in all my scripts to reference the desktop in save commands
set FileVersion to ""
tell application "Finder"
repeat while (exists file (PathToDesktop & YourFolderHere & FileNameVariable & FileVersion))
set FileVersion to (FileVersion & "_2")
end repeat
end tell


Wogo,

Here’s some code you can put in to rename the file. It’s a bit bulky and someone has probably got a much more streamlined version, but it works.

This is just the part for renaming a file. Also if you don’t want to go through the renaming you can add “with replacing” (without quotes) after your move file line.

 move file aFile to "Warp Drive:Other Stuff:SVT" with replacing

This code works well for making sure you have a unique name for every file. The line that sets theDate is code thanks to Kai. I have found it very useful.

I think you can adapt this.

set theFile to choose file with prompt "Choose file to duplicate."
set theDest to choose folder with prompt "Choose destination folder."
tell application "Finder"
	activate
       if not exists folder "weighStation" of desktop then
       set tempDest to make new folder at desktop with properties {name:"weighStation"}
       else
       set tempDest to folder "weighStation" of desktop as alias
       end if
	set fileName to name of theFile
	set astid to AppleScript's text item delimiters -- this saves applescripts original text delimiters.
	tell (current date) to tell (its month as string) & year * 100 + day to set theDate to text -2 thru -1 & text 1 thru 3 & text -4 thru -3 -- thanks to kai. This sets theDate to day, month, year as such 12Dec05.
	set theName to theDate & (time of (current date)) as string -- this sets theName to theDate and current time (in seconds). (12Dec0521544)
	set AppleScript's text item delimiters to "."
	set firstName to first text item of fileName -- this gets the first part of the file name. If the file name is "theFile.mpga" the first part is "theFile". Using applescripts delimeters as set in the previous line separates the text at the period.
	set lastName to ".mp3"
	set newName to firstName & theName &  lastName -- "theFile12Dec0521544.
	set newPos to duplicate theFile to tempDest -- duplicate your file to the folder "weighStation"
	set name of newPos to newName -- set the name of the duplicated file
	move file newName of folder "weighStation" to theDest -- move the new file to it's final destination.
       set AppleScript's text item delimiters to astid
end tell

As I said this is a bit bulky. I couldn’t figure out how to move a file directly into the destination and change the name at the same time. Anyway, if nothing else this gives some examples of things you might find useful.

PreTech

the set newName line sets the new name to something like this “theFile12Dec0521544.mp3”