Newbie: How do I point to/reference a file dynamically?

This is my first attempt at writing an AppleScript. I want to write a script that will basically do a batch process on one or more QuickTime files. Here is what I have thus far:


tell application "QuickTime Player"
	activate
	delete track "Sound Track" of movie "Burner ignite 01"
	save movie "Burner ignite 01"
end tell

I got this by recording my actions. Right now it points to a specific file – “Burner ignite 01” – but I would like to be able to select a few files and have it run as a batch process and save them all to their original directory (overwrite), or have the option of selecting a new location.

I have done PHP coding, so i am not entirely new to programming, but this I have no idea where to go from here. Any and all help will be greatly appreciated!

You can save the following script as an application. Once saved, drop any or all of the movies you want on to the application icon and their soundtracks will be removed:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Can you explain some of this to me?

on run
open {choose file}
end run

This is the obligatory block to make the script an App or Droplet, correct? Then we have

on open the_files

what is the_files? Is this an environmental variable (the plural suggests an array perhaps)? Is this_file a standard file pointer?

      [b][color=blue]if[/color][/b] [color=blue]name[/color] [b][color=blue]of[/color][/b] [color=green]the_info[/color] [b][color=blue]ends with[/color][/b] ".mov" [b][color=blue]or[/color][/b] [color=blue]file type[/color] [b][color=blue]of[/color][/b] [color=green]the_info[/color] = "MooV" [b][color=blue]then[/color][/b] [b][color=blue]my[/color][/b] [color=green]process_this_file[/color]([color=green]this_file[/color])

Is “MooV” a typo? I don’t unerstand that bit.

on [color=green]process_this_file/color
tell application “QuickTime Player”
open this_file
delete track “Sound Track” of movie 1
close movie 1 saving yes
end tell
end process_this_file

You replaced the file name with an integer, but I don’t understand why…

Thanks for the help, and any explanations you can offer to help me better understand the code is greatly appreciated!

Sure.

No, the open handler is the one that makes it a droplet. The run handler is used in case the application is launched from the Finder without any files being dropped on it (i.e., if it was double-clicked from the Finder). When run this way, you will be able to choose a single file that it will coerce to a list (AppleScript terminology for an array) and then pass that list to the open handler. That list is “the_files” that the open handler uses as its input. If you drop one or more files on the icon from the Finder, the file references to those files (even if it is just one file) are passed to the open handler as a list.

As I mentioned above, “the_files” is just a basic list and “this_file” is just a variable to iterate through the list.

No, “MooV” is the file type for QuickTime movies. Traditionally (although somewhat less these days in Mac OS X), the Mac OS assigns a 4-character file type as well as a 4-character creator code to the resource of a file to allow the system to know what type of file a document is as well as what application should open it. Mac OS X has moved to extensions for this and doesn’t rely as much on the file type and creator codes. This line allows you to process QuickTime files if they either have the “.mov” extension or, barring that, if their file type is “MooV”.

Well, as you found above, using a hard-coded name didn’t allow you to vary your code for different file names. Most applications allow you to identify a document by either its name or its index within the application. “Movie 1”, therefore, is a reference to the frontmost document (movie) in the QuickTime player. If you had more than one movie opened, you could reference them by “movie 1”, “movie 2”, etc. In this way you can dynamically act on a file without having to know too much about it.

Hope this helped. Did the code itself work for you?

Jon

I’ll give this a shot.


on run 
     open {choose file} 
end run

The “on run . . . end run” code gets executed when the script starts (unless it is started by dragging a file on to it, but more about that later). This article [url]http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.e4.html/[url] gives some specifics. I’m not too familiar with php, so I’m not sure what to compare it to, but in C it would be like the “main” function.

“choose file” is an Applescript command which will open up a file selection dialog so that the user can choose the files to modify. This list of files is passed to the “open” handler.

This run handler is here so that the user can click on the icon for the script to use it, instead of dragging the files onto the droplet.

The “open” handler is called by the “run” handler as described above if the user clicks on the icon. However, if the user drags files onto the droplet, then the “open” handler is called.


on open the_files 

the_files contains a list of the files to be used. It is a parameter of the “open” handler. It is set either by the “run” handler or by the system when files are dragged onto the droplet.


          if name of the_info ends with ".mov" or file type of the_info = "MooV" then my process_this_file(this_file) 

One of the blessings/curses of the Mac is that a file’s extension is not the only way to specify the file type. I could have a Quicktime movie file that ended in “.txt”. Quicktime movie files have a type of “MooV”. This seems silly, but say it out loud. “Moo”. “V”. “Moo” “V”. “MooV”. (Remember the two-headed monster on Seasame Street?) This code is looking at the file and making sure that it is of the right type. This avoids the problems that you would have if you tried to remove the sound track from a Word document.


on process_this_file(this_file) 
     tell application "QuickTime Player" 
          open this_file 
          delete track "Sound Track" of movie 1 
          close movie 1 saving yes 
     end tell 
end process_this_file 

This part had confused me when I started dealing with AppleScript. When you are inside of a “tell” block, you are talking to the application. Think of it as having a conversation. First, we tell QuickTime Player to “open this_file”. QuickTime Player does that, and it stores a representation of that file in a list of movies that are open. You can refer to this movie as “movie 1”. You don’t have to tell QuickTime Player to do this, it does it automatically. So the second line of the “tell” block is telling QuickTime Player, “Delete the track called ‘Sound Track’ from the first movie in your list of movies that are open.” The third line tells it to save that movie and close it.

One of the hardest things about AppleScript if you come from a language like Pascal or C or PHP is that quite a few things happen without you directly asking for them to happen. It can be kind of scary, especially when you are trying to figure out exactly what it is that the application has done for you. Don’t try to fight it. I almost gave up on AppleScript after hours of trying to access a movie in QuickTime

Hopefully this has helped. I you are looking for a good place to start with AppleScript, try “AppleScript for Absolute Starters” here http://www.applescriptsourcebook.com/tips/AS4AS.html.

First of all, I must thank both jonn8 and aturley for their help. And jonn8, yes the script did work perfectly! However, it turns out that what I want to do in QT is not to “Delete” the audio track. As it turns out, all that does is disable the audio track, it does not remove the audio information, thus my desired filesize savings are not accomplished.

What I need to do is open the movie(s) and extract the video track. I wish to then save this newly created file in the same directory but (for now) append “_VT” to the end of the filename. I tried creating a new script based on what you gave me to work with, but nothing that I have tried is working thus far. Here is what I have:


on run
	open {choose file}
end run

on open the_files
	repeat with this_file in the_files
		set the_info to info for this_file
		set this_folder to alias this_file
		if name of the_info ends with ".mov" or file type of the_info = "MooV" then my process_this_file(this_file)
	end repeat
end open

on process_this_file(this_file)
	tell application "QuickTime Player"
		open this_file
		make new movie with data track "Video Track" of movie 1
		close movie 1 saving no
		--save movie 1 in file "Video HD:Temporary Items:" & this_folder & ":" & 1 & "_VT" as self contained    <-- originally had this, but decided to try line below instead...
		save movie 1 in file this_folder & ":" & 1 & "_VT" as self contained
		close movie 1 saving no
		beep
	end tell
end process_this_file

With the script the way it is right now, when I drop the file “Video HD:Temporary Items:Test Movie” onto the Droplet, I get this error: “Can’t get alias (item 1 of {alias “Video HD:Temporary Items:Test Movie”}).”

I am guessing I am doing something wrong in regards to trying to set the variable this_folder to the folder in which the original file is located.

Just to clarify what it is I am trying to do: I wish to drop movie file(s), say “Video HD:Temporary Items:Test Movie” onto the script that would (via QuickTime Pro) extract the Video Track from “Video HD:Temporary Items:Test Movie” (thus creating a new movie file), then save the new file to the same folder, with the same filename but appending “_VT” to the end of the filename – in my example that would be “Video HD:Temporary Items:Test Movie_VT”.

Again, I am really appreciative of any help I can get!

No, you do want to delete the sound track but then you want to do a save as. This should work:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

I tried the code you posted, but I don’t think it is working…first, it starts the Classic Environment and QuickTime in OS 9, even though QuickTime Pro for OSX is set as the default application. Second, the resulting file is only 8 KB and has a length of 00’00.

I don’t have QT for Classic installed on my machine so it correctly launches the OS X version and the files it saves are correctly saved without the audio track. I’m using QT Pro 6.4 on Mac OS X 10.3.1.

Jon

Works fine here… (same OS/QT).
What if you replace this handler?

on process_this_file(this_file)
	tell application ((path to applications folder as text) & "QuickTime Player.app")
		using terms from application "QuickTime Player"
			open this_file
			delete track "Sound Track" of movie 1
			save movie 1 in file (my get_new_name(this_file)) as self contained
			close movie 1 saving no
		end using terms from
	end tell
	beep
end process_this_file