Help with script

Hi there,

I’m just kinda getting started with Applescript and have written some short scripts that help me with bits. My latest project is a little bigger and more adventurous for me.

I’m trying to write a script that will export a pict file from a QTVR movie and therefore involves Quicktime Player. I’ve downloaded some scripts from Apple and have tried customizing one that already does what I want.

So far the script is looking like this:-

displayName(choose file with prompt “Select a file:”) --if double-clicked
return – not needed, but shows that the script stops here when “run”

on open of finderObjects – catches drag’n’drop launches
repeat with i in (finderObjects) – in case multiple objects dropped on applet
processName(i) – show file/folder’s info
end repeat
end open

on processName(theFile) – an example of doing something with each item
tell application “QuickTime Player”
activate
open theFile
set file_name to theFile as string
set this_file to new file default name (file_name & “.pct”)
set the chosen_preset to “Photo-JPEG”
export movie 1 to this_file as picture using settings preset chosen_preset
end tell
end processName

The script exports pict files from any movies dropped on it which is all ok. However, to make the script more of a batch process I have to leave my coffee cup positioned on the RETURN key. Firstly this isn’t very good scripting wise and secondly I can’t get a cup of coffee whilst this is working!

There are a number of things I’d like to change.

  1. How do I get rid of the need to press the RETURN key to SAVE the file? Can this be done? Can the save dialog box be bypassed in some way?

  2. I’d like to take the idea one stage further so I have two folders, ‘TO PROCESS’ and ‘PROCESSED’. I drop the file in ‘TO PROCESS’ and it is saved in the ‘PROCESSED’. Originally I was going to ask how I could amend thie line below

‘export movie 1 to this_file as picture using settings preset chosen_preset’

of the script to allow me to save the file to a specified folder. I have tried but as I said I’m newish to Applescript so couldn’t get it to work. If anyone can help me there I’d be greatful.

Thanks in advance for any help with this, any help would be greatly appreciated.

Nick H

Nick,

Your script would not compile for me - not sure if you are using the same software as me. I am running QT Player 5.0.2 under OS 9.2 .

Anyway - I was able to produce something - hopefully it will help you along.
the following script works when double clicked, when you drop files, or a folder on it,
and if saved as a stay-open application will watch a drop folder, like you wished.
I added a folder to the construct so now it includes a watched “To Process” folder, a folder for the exported picts, and a folder for movies that have been processed.

Here it is:

property dropFolder : "Mac HD:Desktop Folder:To Process:"
property processedFolder : "Mac HD:Desktop Folder:Processed:"
property pictFolder : "Mac HD:Desktop Folder:Picts:"

on run
	set dropFolder to (choose folder with prompt "Select a folder:") --if double-clicked have user select a folder of movies
	set processThese to list folder dropFolder without invisibles
	my processName(processThese) --process the files found
end run

on open of processThese -- catches drag'n'drop launches, determines folder or files and acts accordingly
	if the number of items in processThese = 1 and folder of (info for processThese) = true then --if there is only 1 item, and it is a folder - get the contents and process those
		set processThese to list folder processThese without invisibles
	end if
	my processName(processThese) -- process the files found using this subroutine 
end open

on idle --if saved as a stay-open script, will check when the computer is idle
	set processThese to list folder dropFolder without invisibles
	if the number of items in processThese > 0 then --then there are items in there
		my processName(processThese) --process the files found
	else
		return 3 --if there is nothing there check back in 3 seconds
	end if
end idle

on processName(processThese) -- an example of doing something with each item 
	repeat with thisFile in processThese --repeat with every file found
		set thisFile to thisFile as string --thisFile was previously {item 1 of whatever list of items we found}  coerce to text
		set thisPath to dropFolder & thisFile as string --define the path to the file
		tell application "QuickTime Player"
			activate
			open thisPath
			set this_file to pictFolder & thisFile & ".pct" as string
			set the chosen_preset to "Photo-JPEG"
			try
				export movie 1 to this_file as picture using settings preset chosen_preset
			on error --QT Player will get an error if the file already exists
				--possibly change the name to something that doesn't exist, I'll let you decide
				--and maybe log the error to a file, or email the error to someone
			end try
			close movie thisFile saving no
			tell application "Finder" to move file thisPath to folder processedFolder with replacing --move the file we just processed out of the drop folder so it isn't processed again
		end tell
	end repeat
end processName

I tested all 3 methods and it did work for me.

Thanks for the help with this small project. I think I must be doing something wrong though. I’ve copied the script into the Editor and saved it as a ‘classic applet’ however when I drop a file on the droplet Quicktime movie player opens and then I get an alert

‘MM_G3_EB:Desktop Folder: Exported Picts:filename doesn’t understand the close message’

Do I need a scripting addition present to enable the code to work? Could it be you have something installed that I don’t?

I’ve tried a number of variations but can’t seem to get the code to work. I’ve also tried amending the three properties at the top of the script but still no luck

I have the three folders ‘To Process’, ‘Processed’ and ‘Picts’ on my desktop. The HD is called MM_G3_EB, Any ideas on what could be causing the problem (besides my limited knowledge of Applescript).

I even went as far as updating the Mac OS to ensure I was running the same as you but still no luck.

Any help would be greatly appreciated, although the code I received from the last post is more than I expected, thanks again.

Nick H

No additional OSAX should be needed to run this script.

Sorry, failure on my part - not yours. I apologize for any frustration.
I must not have tested dropping files onto the droplet. All other methods
pass file names to the processName subroutine. But when you drop a file(s)
onto it - the script passes a list of aliases.

I fixed it by checking later if thisFile contains a colon. If so, it indicates
we are looking at a file path, and not a file name. Then I have the finder
get the name of that file. That is why you ran into errors with the close
command. It should tell Quicktime to close a document by name. By your
error message I can see we were having it look for a document named:

Here it is again - fixed. You should only have to fix the properties to reflect your
computer name - and folder names.

Again, very sorry.

property dropFolder : "TWS-0103:Desktop Folder:To Process:"
property processedFolder : "TWS-0103:Desktop Folder:Processed:"
property pictFolder : "TWS-0103:Desktop Folder:Picts:"
property otherFolder : ""

on run
	set dropFolder to (choose folder with prompt "Select a folder:") --if double-clicked have user select a folder of movies
	set processThese to list folder dropFolder without invisibles
	my processName(processThese) --process the files found
end run

on open of droppedItems -- catches drag'n'drop launches, determines folder or files and acts accordingly
	if the number of items in droppedItems = 1 and folder of (info for droppedItems) = true then --if there is only 1 item, and it is a folder - get the contents and process those
		set processThese to list folder droppedItems without invisibles
		tell application "Finder"
			set otherFolder to droppedItems as string --get the name of the folder that was dropped
		end tell
	else
		set processThese to droppedItems
	end if
	my processName(processThese) -- show file/folder's info 
end open

on idle --if saved as a stay-open script, will check when the computer is idle	
	set processThese to list folder dropFolder without invisibles
	if the number of items in processThese > 0 then --then there are items in there
		my processName(processThese) --process the files found
	else
		return 3 --if there is nothing there check back in 3 seconds
	end if
end idle

on processName(processThese) -- an example of doing something with each item 
	
	repeat with thisFile in processThese --repeat with every file found
		set thisFile to thisFile as string --thisFile was previously {item 1 of whatever list of items we found}  coerce to text
		
		if thisFile contains ":" then --then it was dropped on the script and was passed to this handler as a file path instead of a name - get the name
			tell application "Finder"
				set otherFolder to the folder of file thisFile as string --then the drop folder will be different that the one specified above.
				set thisFile to the name of file thisFile
			end tell
		end if
		if otherFolder is not equal to "" then --then the file resides in a place other than the drop folder defined in the property
			set thisPath to otherFolder & thisFile as string --define the path to the file
		else
			set thisPath to dropFolder & thisFile as string --define the path to the file
		end if
		tell application "QuickTime Player"
			activate
			open thisPath
			set this_file to pictFolder & thisFile & ".pct" as string
			set the chosen_preset to "Photo-JPEG"
			try
				export movie 1 to this_file as picture using settings preset chosen_preset
			on error --QT Player will get an error if the file already exists
				--possibly change the name to something that doesn't exist, I'll let you decide
				--and maybe log the error to a file, or email the error to someone
			end try
			close movie thisFile saving no
			tell application "Finder" to move file thisPath to folder processedFolder with replacing --move the file we just processed out of the drop folder so it isn't processed again
		end tell
	end repeat
	set otherFolder to "" --reset this property
end processName

I also failed to test if the files were dropped from a different folder. The variable dropFolder would be different if located in a different folder than the predefined drop folder - this would cause the Open command to fail. I added a line to get the folder path if files are dropped onto it.

I think this should work now.


edited yet again.

Well - I did some more testing and found

a) I’m not very good at testing things
b) it still didn’t work very well.

I fixed some other problems with dropping files and folders on it - when the parent folder was something other than the predefined drop folder it wigged out.

This should be it - thatnks for your patience,

Thanks again for the updated script, it has been a learning exercise for me. No apologies are necessary, I’m greatful for the help.

Unfortunately once again I had problems with the script. I amended the first three properties for my HD (swapped the TWS-0103 for MM_G3_eb) however when I drop a file on the applet, even from the ‘To Process’ folder, I get the error:-

movie “360quicktimetest1.mov” doesn’t understand the close message

When I click on EDIT the script editor highlights this line of code:-

close movie thisFile saving no

Nothing has been exported from Quicktime even though the script has reached this point which is a few lines after the export line. Could this have anything to do with a coercion problem?

Am I doing something wrong here?

I’ve checked my system and it’s Mac OS 9.2 and Quicktime player 5.02 which looking through previous posts is what you’re running.

Any ideas?

Sorry to be a pain with this,

thanks again for the help.

Regards

Nick H

Nick,

Let’s examine it step by step. The script works as is on my station with the same software you are running. Confusing but maybe we can isolate where the problem is.

“360quicktimetest1.mov” must be the movie you are processing at the time of the error. Is it open when you get the error message regarding not understanding the close statement? That is a direct error from Quicktime Player and it should only happen if there is no window open, or if the movie you have open doesn’t have the same name as the one you are telling it to close.

I wrote 3 small scripts to try to see if we can isolate the error.

set thisPath to "Mac HD:Desktop Folder:SomeMovie.MPG"--change this to match your computer
tell application "Finder" to set fileName to the name of file thisPath
tell application "QuickTime Player"
	activate
	open thisPath
end tell
--does it open the movie?

--now add the export commands and run it again
set thisPath to "Mac HD:Desktop Folder:SomeMovie.MPG"--change this to match your computer
set pictFolder to "Mac HD:Desktop Folder:Picts:"--change this one too, make sure the folder has the same name and is on the desktop
tell application "Finder" to set thisFile to the name of file thisPath
tell application "QuickTime Player"
	activate
	open thisPath
	set this_file to pictFolder & thisFile & ".pct" as string
	set the chosen_preset to "Photo-JPEG"
	--omit the try, on error (we want to see what error is occuring to make it skip export)
	export movie 1 to this_file as picture using settings preset chosen_preset
end tell
--does it export?  If not, what error are you getting when it tries to export?


--now add the close commands and run it again
set thisPath to "Mac HD:Desktop Folder:SomeMovie.MPG"--change this to match your computer
set pictFolder to "Mac HD:Desktop Folder:Picts:"--this one too
tell application "Finder" to set thisFile to the name of file thisPath
tell application "QuickTime Player"
	activate
	open thisPath
	set this_file to pictFolder & thisFile & ".pct" as string
	set the chosen_preset to "Photo-JPEG"
	--omit the try, on error (we want to see what error is occuring to make it skip export)
	export movie 1 to this_file as picture using settings preset chosen_preset
	close movie thisFile saving no
end tell
--does it close the movie, or do you still get the error?

In these scripts we are hard coding an actual file that is on your desktop
instead of jumping thru a bunch of hoops and passing file paths, and file
names. If all 3 of those work then maybe we need to focus more on what we
are passing to Quicktime.

Anyway, test this out - and post your results…

Best,

Sorry for the delay with the reply, you were probably hoping for the results of the scripts.

Well, I tried the first script and this could be where the problem is, the file didn’t open in quicktime. I checked the filename and path however no luck. I did try amending the code slightly so the filename and path were set up further down the code and this seemed to work, I’m not quite sure how I managed to do it.

I checked the file path using another little script and that is correct.

Any ideas.

Thanks again for the help

set thisPath to "Mac HD:Desktop Folder:SomeMovie.MPG"--change this to match your computer 
tell application "QuickTime Player" 
   activate 
   open thisPath 
end tell

If you’ve got line 1 correct, this should open your movie. Change Mac HD to the name of your startup disk, and change the rest of the file path to match where your file really is. It really shouldn’t matter where this line is placed - it just needs to be before you try to have QT open the movie.

See if you can get this to work. We’ll move on from there.

Thanks again for looking at the problem.

Again the script still won’t the file. This is what I can’t understand, I’ve gone as far as renaming the HD “Mac HD”, so line 1 of the script reads:-

set thisPath to “Mac HD:Desktop Folder:qtvr4.mov”–change this to match your computer

This now matches the path of the file in question. On running the script quicktime activates - no problem there - but it doesn’t open the file. I don’t get any errors from the script.

Even with my very basic knowledge of applescript I can’t understand why the file can’t be found and opened. This is probably a silly question but is the variable ‘thisPath’ a global one, can it be accessed by quicktime. This is obviously one thing I need to try and sort. As far as I can see the path is correct but the problem is still there.

I’ll keep you posted.

Thanks

Nick

What happens if you change ‘open thisPath’ to:

open alias thisPath

:smiley:
Wonderful - that works, the file is opened in quicktime player and views ok.

I guess I should amend the code accordingly in the three test files you posted. Or then again should the amended code you posted work ok if this change is made.

Thanks again, I think you’ve cracked it!

Nick

P.S. Hhhhmmmm. Just for my reference and advancing my knowledge of Applescript, why does the file in question have to opened as an alias?

Hi Ray,

I amended the code in each of the three test scripts to read ‘open alias thisPath’ and tried executing each one.

The first script worked ok - the movie opened.

The second script failed with the error message ‘Can’t make some data into expected type.’ Is this due to a coercion problem with the file or filename of the movie being processed?
The script editor highlighted whis line of code:-

export movie 1 to this_file as picture using settings preset chosen_preset

Because we’ve changed the ‘open thisPath’ to ‘open alias thisPath’ is this ‘set this_file to pictFolder & thisFile & “.pct” as string’ line of code causing the coercion problem?

As for the third I didn’t try this.

I did try the amending the second version of the original application that you wrote for me, I added the ‘alias’ bit to the relevant line. When I dropped a qtvr movie on the droplet the file opened in Quicktime and then closed. The original file was moved to the folder ‘Processed’ however no pict file was exported to the ‘Pict’ folder. No error message was generated.

I’ll try some tinkering, if you’ve any thoughts they’d be appreciated.

Thanks

Nick

Strange. The open line works for me without ever having to define it as an alias. Just odd since we are basically running the same systems.

It sounds like you are very close now - the script opens your movies, and closes them and moves them to the processed folder. It appears we just need to get the export to work.

This is definitely the line we need to get working. I can only think of a couple of things to try.

  1. Since you had to define thisPath as an alias, maybe we need to define this_file as an alias as well in order to get the export to work. Currently, at that point, this_file is the path to what will be the pict file, (as text). Maybe, try changing this line to:
export movie 1 to alias this_file as picture using settings preset chosen_preset

2)I’m thinking #1 should fix the export problem. But if it doesn’t, maybe we need to see what your script thinks “this_file” is before trying to export. If #1 doesn’t do the trick, try placing the following line just before the export line.

display dialog this_file

It should display a dialog that contains a path to the pict file that will be saved. Take a look at the full path to see if there is anything wrong with it.

Again, I think #1 should do the trick but still find it totally odd that you had to define the file to open as an alias.

Good luck!

I tried amending the code to include ‘alias’ however an error is generated telling me that the file can’t be found.

Whilst trying this I had a look at my original code that still works:-

displayName(choose file with prompt “Select a file:”) --if double-clicked
return – not needed, but shows that the script stops here when “run”

on open of finderObjects – catches drag’n’drop launches
repeat with i in (finderObjects) – in case multiple objects dropped on applet
processName(i) – show file/folder’s info
end repeat
end open

on processName(theFile) – an example of doing something with each item
tell application “QuickTime Player”
activate
open theFile
set file_name to theFile as string
set this_file to choose file name default name (file_name & “.pct”)
set the chosen_preset to “Uncompressed”
export movie 1 to this_file as picture using settings preset chosen_preset
close movie 1
end tell
end processName

This particular line seems to make all the difference:-

set this_file to choose file name default name (file_name & “.pct”)

When you drop a file on this droplet you are prompted for a file name, the default is set to the filename + “.pct”. On press the RETURN key the file is saved to same place as the original but with the file extension .pct.

If I try to change the above line that’s when I start to run into problems.

I’ll have to keep playing

Mytzlscript,

If I had to bet, I’d bet that you have an osax that is silently coercing the string to alias. If I’m not mistaken, there are a couple that do this. It can be a real PIA in situations like this. :slight_smile:

– Rob

set thisPath to "Mac HD:Desktop Folder:To Process:SomeMovie.MPG"
tell application "QuickTime Player"
	activate
	open thisPath
end tell

Interesting. I moved all my scripting additions out my desktop, relaunched Script Debugger and this still works for me. Then I decided to determine if it was Script Debugger that was coercing it for me so I saved it out as a classic app. Still works.

Would be nice to know how that happens, especially if I am trying to help people.

Depending on the version of Script Debugger in use, you might be able to use the Manifest report to find which, if any, osaxen are being used. If silent coercion isn’t the problem, I don’t have any idea why the two systems are behaving differently and I don’t know why your system will open a string. Finally, simply moving the osaxen to the desktop might not be enough. It might require a restart to see the full effect of disabling the osaxen. I’ve been using OS X long enough that I’ve forgotten a lot about pre-OS X stuff. :stuck_out_tongue:

I do have SD3 and the manifest reports that absolutely no Scripting Additions are being used. Still works after restart too. All of my applications behave that way too- am I going mad?

:smiley: I don’t know if you are going mad but it sure seems odd. Do the scripts work when run outside of Script Debugger? I didn’t really think that the manifest would catch a silent coercion but it was worth a shot. I haven’t had my recommended daily dose of caffeine yet so maybe this will become less puzzling after I drink a few more pots of coffee. :wink:

Rob,

Thanks for having a look at this scripting problem. Could there be some sort of scripting addition causing a problem on my system?

Thanks