Creating folders - differences between applet and script

I have made a script that lets the user choose a folder (A), creates a subfolder (B) with the same name as the containing folder (A), uses QT Pro to combine audio and video files within the folder (A) and then saves them in the subfolder (B). The script works. However, when I save the script as an applet (with what I think is the appropriate change) and then dragging a folder on top of it, it returns the error “Can’t make class <>.”

As the error suggests, the line of code it has a problem with is the one in which I create the subfolder. Here is the script (the troublesome line is the one immediately after “tell application Finder”):


on open mainFolder
	
	-- set extensions for input and output audio and video files
	set audExt to "wav"
	set vidFromExt to "mov"
	set vidToExt to "mov"
	set nameExt to "Full"
	
	-- create a subfolder with the same name as the containing folder
	set folderPath to mainFolder as text
	set revFolderPath to (rest of (reverse of (characters of folderPath))) as string
	set theOffset to offset of ":" in revFolderPath
	set thePrefix to (reverse of (characters (theOffset + 1) thru -1 of revFolderPath)) as string
	set folderName to (reverse of (characters 1 thru (theOffset - 1) of revFolderPath)) as string
	
	
	tell application "Finder"
		
		-- create folder with given name
		-- *** THE NEXT LINE IS THE PROBLEM ***
		set saveToFolder to make new folder at mainFolder with properties {name:folderName}
		set saveToFolderName to name of saveToFolder
		
		-- all video files in the folder
		set allVidFiles to (every file of folder mainFolder whose name extension is vidFromExt)
		
		-- fileNum will be used to iterate through the names of video files
		set fileNum to 1
		
		-- iterate over every video file
		repeat with nextFile in allVidFiles
			
			-- identify the paths of the audio and video files
			set nextPath to (name of item fileNum of allVidFiles)
			set nextName to (characters 1 thru -5 of nextPath as text)
			set audPath to (mainFolder & nextName & "." & audExt as text)
			set vidPath to (mainFolder & nextPath as text)
			
			-- QT magic!
			tell application "QuickTime Player 7"
				activate
				
				-- copy audio track
				set audFile to open audPath
				rewind audFile
				select all audFile
				copy audFile
				
				-- prepare video track for audio overlay
				set vidFile to open vidPath
				rewind vidFile
				select all vidFile
				add vidFile
				rewind vidFile
				
				--save and close files
				close audFile
				export vidFile to file (folderPath & folderName & ":" & nextName & nameExt) as QuickTime movie
				close vidFile
				
			end tell
			
			set fileNum to (fileNum + 1)
			
		end repeat
		
	end tell
end open

The only difference between this applet version of the script and the working non-applet version is that in the latter, the open/end open statements are replaced by a single line at the beginning of the script:

set mainFolder to choose folder with prompt "Choose a subject folder."

What do I need to do differently when the script is an applet to get it to work?

Thanks,
Dan

hi,

mainFolder you get from “on Open” is class list.

so expecting one Folder to be dropped it should be item 1 of mainFolder.

unexpected from now on is, that the class of mainFolder is “bmrk” (bookmark URL) and not “alias”.
would be nice to get some explanation about this …

finally, if you change the line:

		set saveToFolder to make new folder at alias mainFolder with properties {name:folderName}

it should work - hope so … :wink:

It seems that class bookmark URL behaves as an alias.
You can coerce it to text and it responds to the POSIX path property.

Coercing «class bmrk» to number, which is of course silly, generates the error message
Can’t make alias “Path:to:file” into type number

I guess the bookmark class conforms to the new bookmark reference feature of NSURL/CFURLRef.
In AppleScript you can treat it as an alias

This is a simpler version of the script with some error handling (folder exists and no folder dropped)


on open theItems
	if folder of (info for item 1 of theItems) is false then return -- no folder dropped
	set mainFolder to item 1 of theItems
	-- set extensions for input and output audio and video files
	set audExt to "wav"
	set vidFromExt to "mov"
	set vidToExt to "mov"
	set nameExt to "Full"
	
	tell application "Finder"
		
		-- create a subfolder with the same name as the containing folder
		set folderName to name of container of mainFolder
		if not (exists folder folderName of mainFolder) then
			set saveToFolder to make new folder at mainFolder with properties {name:folderName}
		end if
		set allVidFiles to (every file of mainFolder whose name extension is vidFromExt)
	end tell
	
	set mainFolderPathString to mainFolder as text
	repeat with nextFile in allVidFiles
		
		-- identify the paths of the audio and video files
		set nextName to text 1 thru -5 of (get name of nextFile)
		set audPath to mainFolderPathString & nextName & "." & audExt
		set vidPath to mainFolderPathString & nextFile as text
		
		-- QT magic!
		tell application "QuickTime Player 7"
			activate
			
			-- copy audio track
			set audFile to open audPath
			rewind audFile
			select all audFile
			copy audFile
			
			-- prepare video track for audio overlay
			set vidFile to open vidPath
			rewind vidFile
			select all vidFile
			add vidFile
			rewind vidFile
			
			--save and close files
			close audFile
			export vidFile to file (folderPath & folderName & ":" & nextName & nameExt) as QuickTime movie
			close vidFile
			
		end tell
		
	end repeat
end open


hi Stefan,

thx for your explanation :slight_smile:

I’m here on osx 10.7.2, AS 2.2.1 and AS-Ediditor 2.4.1

you’re lines always throw an error ““Finder” hat einen Fehler erhalten. Klasse “Folder” kann nicht erstellt werden” (Good old German System ;-))

on open theItems
	if folder of (info for item 1 of theItems) is false then return -- no folder dropped
	set mainFolder to item 1 of theItems
	tell application "Finder"
		
		set folderName to name of container of mainFolder
		if not (exists folder folderName of mainFolder) then
			try
				set saveToFolder to make new folder at mainFolder with properties {name:folderName}
			on error e
				display dialog e
			end try
		end if
	end tell
	
end open


Have to coerce it to “alias” or “folder” like in my previous post …

This strikes me wonder …

I just tested the script on Lion and it worked fine

Hans-Gerd, adding “alias” made the droplet work fine, though I’m still not certain I understand why it worked without it in script form.

Stefan, thank you for your revision of my script. It didn’t quite work for me as-is (I had to add “alias” again for it to compile and changed how it set the name of the containing folder), but it was easy to modify. I also made it so it can take several items at once. I’m posting it here since the finished product is as much your contributions as mine.

Regards,
Dan

on open theItems
	
	-- set extensions for input and output audio and video files
	set audExt to "wav"
	set vidFromExt to "mov"
	set vidToExt to "mov"
	set nameExt to "Full"
	
	repeat with mainFolder in theItems
		
		-- do something with that item iff it is a folder
		if folder of (info for mainFolder) is true then
			
			tell application "Finder"
				
				set allVidFiles to (every file of mainFolder whose name extension is vidFromExt)
				
				-- create a subfolder with the same name as the containing folder
				set folderName to name of container of (item 1 of allVidFiles)
				if not (exists folder folderName of mainFolder) then
					set saveToFolder to make new folder at alias mainFolder with properties {name:folderName}
				end if
			end tell
			
			set mainFolderPathString to mainFolder as text
			repeat with nextFile in allVidFiles
				
				-- identify the paths of the audio and video files
				set nextName to text 1 thru -5 of (get name of nextFile)
				set audPath to mainFolderPathString & nextName & "." & audExt
				set vidPath to nextFile as text
				
				-- QT magic!
				tell application "QuickTime Player 7"
					activate
					
					-- copy audio track
					set audFile to open audPath
					rewind audFile
					select all audFile
					copy audFile
					
					-- prepare video track for audio overlay
					set vidFile to open vidPath
					rewind vidFile
					select all vidFile
					add vidFile
					rewind vidFile
					
					--save and close files
					close audFile
					export vidFile to file (mainFolderPathString & folderName & ":" & nextName & nameExt) as QuickTime movie
					close vidFile
					
				end tell
			end repeat
		end if
	end repeat
end open

Hi,

if “bmrk” behaves as “alias” on one lionMachine but not on the other … is this a bug or a change in the lionHistory¿

As mentioned, on my machine “ which is running 10.7.2 “ I have to coerce the class “bmrk” to “alias” to treat it like an alias …

Will this work for all lionMachines (and older snowLeoMachines) or will it throw an error¿


on open dropped
set theAlias to (first item of dropped) as alias
display dialog (class of theAlias)
end open

Sorry, but I’m a bit confused about this behaviour …

Maybe Script Debugger is the reason that it works on my machine.

It seems that Script Debugger compiles the on open parameter implicitly as list of alias specifiers.
An explicit coercion to alias is indeed the most reliable solution.

FWIW, the change to bookmarks happened in one of the later Snow Leopard versions, not Lion. And as Stefan surmises, Script Debugger still passes aliases.