Coercion probelm

Hello,

I cannot seem to get a handle on how to coerce data! This time, I am trying to make a droplet for kids to drop mp3 files onto. It is very important that the names of these files are correct, so I put in an if/then to make sure the file names are correct. The relevant part of the script is below.

QUESTION #1
How do I get the name of the file(s) being turned in? In the event that they turn in an incorrectly named file, I want to be able to notify them with something like “Hey, the file FILENAME is named wrong…go back and fix it.” Testing this script, I turned in a file called “puzzle.jpg” from my desktop. This is not an approved file name, so the script should tell me this and exit. Instead, I get the error message “Can’t make name of alias (item 1 of {alias “Hard_Drive:Users:huber:Desktop:PUZZLE.jpg”}) of application “Finder” into type string.”

Can you see what I am doing wrong? I have tried other language besides “set CurrentFileName to name of alias (CurrentFile) as text,” but it has failed, too.

Thank you,

-Dave



--turn in each item
	repeat with CurrentFile in draggedItems
		
		--get the current file name
		tell application "Finder" to ¬
			set CurrentFileName to name of alias (CurrentFile) as text
		
		
		--check if it is one of the approved names.  If not, display error.
		if (CurrentFileName = "Atlantis.mp3" or ¬
			CurrentFileName = "Bonus.mp3" or ¬
			CurrentFileName = "China.mp3" or ¬
			CurrentFileName = "Completion.mp3" or ¬
			CurrentFileName = "Crete.mp3" or ¬
			CurrentFileName = "Night.mp3" or ¬
			CurrentFileName = "Theme.mp3") then
			tell application "Finder"
				duplicate CurrentFile to DropBox replacing yes
			end tell
		else
			tell application "SystemUIServer"
				activate
				display dialog "Sorry, the song '" & CurrentFileName & "' is named wrong.  Go back and fix it." buttons {"Oops"} default button 1 giving up after 10
			end tell
		end if
	end repeat

How’s about like this… though I would prolly remove the application tell block from the repeat.

on open draggedItems
	repeat with CurrentFile in draggedItems
		tell application "Finder" to set CurrentFileName to name of CurrentFile
		(*
		
		rest of code
		
		*)
		
	end repeat
end open

That did it. THANKS! I thought for sure I tried something like that, but obviously I did not…

Thanks again,

Dave

Hi Dave,

it’s easier (and faster) to check the condition with a list instead of a couple of or-statements
like

...
	if (CurrentFileName is in {"Atlantis.mp3", "Bonus.mp3", "China.mp3", ¬
		"Completion.mp3", "Crete.mp3", "Night.mp3", "Theme.mp3"}) then
		tell application "Finder"
			duplicate CurrentFile to DropBox replacing yes
		end tell
...

also in your duplication statement you are already within a Finder tell block so you shouldn’t have to replicate it also no need to tell SystemUIServer for the dialog so perhaps something like this

untested

on open draggedItems
	tell application "Finder"
		repeat with CurrentFile in draggedItems
			set CurrentFileName to name of CurrentFile
			if CurrentFileName is in {"Atlantis.mp3", "Bonus.mp3", "China.mp3", "Completion.mp3", "Crete.mp3", "Night.mp3", "Theme.mp3"} then
				duplicate CurrentFile to DropBox replacing yes
			else
				display dialog "Sorry, the song '" & CurrentFileName & "' is named wrong.  Go back and fix it." buttons {"Oops"} default button 1 giving up after 10
			end if
		end repeat
	end tell
end open

Thanks so much for the help! Originally, the post had read “coercion and if/then statements” and I had inquired about the If/thens, but then I thought I was being greedy so I deleted it.

Thanks again, this is a big help. This is the most helpful and responsive forum I have ever participated in!

-Dave