Help with AppleScript

Ask questions and get answers on how to make Max behave.
Post Reply
nathanziarek
Posts: 4
Joined: Tue Jan 10, 2006 9:14 pm

Help with AppleScript

Post by nathanziarek » Thu Jul 19, 2007 2:49 am

In leiu of my other question regarding a command line interface, can someone point me to better documentation of the AppleScript interface Max offers? I simply want to take a file and convert it to AAC @ 96K VBR, but, other than:

Code: Select all

tell application "Max"
activate
convert...
end tell
I have no idea how to use convert.

Any help is appreciated!

Nate

User avatar
sbooth
Site Admin
Posts: 2445
Joined: Fri Dec 23, 2005 7:45 am
Location: USA
Contact:

Post by sbooth » Thu Jul 19, 2007 4:51 am

The AppleScript interface is likely to fall short of your needs. I haven't fully developed it, nor tested it at all.

So, any help in designing/improving it would be appreciated.

nathanziarek
Posts: 4
Joined: Tue Jan 10, 2006 9:14 pm

Post by nathanziarek » Thu Jul 19, 2007 12:43 pm

I'd love to be able to help, but I couldn't code my way out of a paper bag. I can barely AppleScript (although, to be fair to me, the psuedo-sentence like syntax is hard to understand)...

I can tell you what I am attempting, though, if you find someone that can help you out.

I am basically trying to use Max to take my iTunes Library of Lossless, tagged, album-arted files and convert them to lower quality AACs for my iPhone/iPod. Selecting them all in Max and converting is a good first step, but I want something more automated that runs nightly. I also need something to convert my video files to smaller resolutions and subsequently rewrite the iTunes DB to reflect the changes in size or other attributes.

So, my idea (baed on the fact that Max is awesome, and has no command line) was a combination of PHP to rewrite the XML, AppleScript to control Max and the audio conversion and ffmpeg for the video stuff.

Ideally, I'd like the AppleScriptable Max to be able to do some pretty simple stuff something like:

Code: Select all

set my_file to alias "Media Drive 200GB:Music:iTunes:iTunes Music:Barenaked Ladies:Stunt:01 One Week.m4a"

tell application "Max"
activate
set output_format to "MPEG 4 Audio"
set encoder_settings to "AAC m4a 96k maximum"
set output_folder to "..."
convert my_file using output_format encoder_settings to output_folder
end tell
And then it would run.

Does any of that make sense?[/code]

User avatar
sbooth
Site Admin
Posts: 2445
Joined: Fri Dec 23, 2005 7:45 am
Location: USA
Contact:

Post by sbooth » Thu Jul 19, 2007 1:38 pm

That does make sense. Unfortunately that level of automation isn't possible with the current AppleScript interface- I will open a feature request for improved AppleScript-ability.

neil.baldwin
Posts: 23
Joined: Sun May 13, 2007 9:58 pm

Re: Help with AppleScript

Post by neil.baldwin » Mon Jul 07, 2008 11:20 am

Stephen,

Have you made any progress on Applescript functionality?

I'm trying to write an Applescript that pulls a ton of WAV files from a folder structure and converts them (to AIFF) using Max. I can use the "convert" command i.e.

Code: Select all

tell application "Max"
    (set "theFile" to valid WAV file)
    convert theFile
end tell
but all that does is add the filename in theFile to Max's conversion widow and doesn't actually carry out the conversion.

Neil

neil.baldwin
Posts: 23
Joined: Sun May 13, 2007 9:58 pm

Re: Help with AppleScript

Post by neil.baldwin » Mon Jul 07, 2008 7:37 pm

I made some progress through GUI scripting Max.

Code: Select all

tell application "Finder"
	set theFile to choose file
	
	tell application "Max"
		activate
		convert theFile
	end tell
	
	delay 1
	
	tell application "System Events"
		tell process "Max"
			click button "Convert" of tool bar of window "File Conversion"
		end tell
	end tell
	
end tell

Which works but I could do with a method to poll the Encoder window to see if it still has any active jobs. Does anyone know if this is possible?

The "delay 1" command seemed to be necessary when I was testing to stop "System Events" from trying to click the "Convert" button before Max had actually added a file to the job list.

Oh, to enable GUI scripting, you need to turn it on;

System Preferences->Universal Access->Enable Access For Assistive Devices = on

neil.baldwin
Posts: 23
Joined: Sun May 13, 2007 9:58 pm

Re: Help with AppleScript

Post by neil.baldwin » Mon Jul 07, 2008 8:01 pm

New version. I figured out that you can do a simple check in AS to see if the Encoder window is still open. If you have Max preferences set to hide the Encoder window on completion of the job(s), this will enable the script to wait until Max has finished processing before continuing. Can easily be expanded to convert multiple files or the contents of multiple folders etc.

If anyone is interested in a more functional version let me know and I'll put something together.

Code: Select all

--
-- Use GUI scripting to convert a file in Max
--
-- script waits for conversion to finish before quitting
---
tell application "Finder"
	set theFile to choose file
	
	tell application "Max"
		convert theFile
	end tell
	
	delay 1
	
	tell application "System Events"
		tell process "Max"
			
			-- Start conversion
			click button "Convert" of tool bar of window "File Conversion"
			
			--Use error trap to check is Encoder is still open
			set encoderOpen to true
			repeat while encoderOpen is true
				try
					window "Encoder"
				on error
					set encoderOpen to false
				end try
			end repeat
		end tell
	end tell
end tell

User avatar
sbooth
Site Admin
Posts: 2445
Joined: Fri Dec 23, 2005 7:45 am
Location: USA
Contact:

Re: Help with AppleScript

Post by sbooth » Mon Jul 07, 2008 9:30 pm

That's pretty slick. It's a nice workaround for the somewhat lacking AppleScript support in the current version of Max.

neil.baldwin
Posts: 23
Joined: Sun May 13, 2007 9:58 pm

Re: Help with AppleScript

Post by neil.baldwin » Mon Jul 07, 2008 10:03 pm

sbooth wrote:That's pretty slick. It's a nice workaround for the somewhat lacking AppleScript support in the current version of Max.
Thanks :)

I realise that to process a folder of files you can just use Max to do the job (drag and drop the folder into the File Conversion window etc.) but I needed a specific function because I'm doing some rather nutty stuff with Applescript on my sound effects library since I found out about how brilliant Spotlight is if you've got properly tagged media.

All my sound effect libraries are in WAVs at the moment with "metadata" (description of content) in the Spotlight comment for each file. To get proper metadata the files have to be AIFF (WAV format doesn't properly support it) so I needed something to convert every WAV to AIFF but also to copy the Spotlight comment from the WAV to the AIFF before removing the WAV. I then script iTunes to import the new AIFFs, grab the Spotlight comment and stick it in the Comment field for each one.

This is where scripting Max came in as I can now process the whole library, one WAV file at a time, all automatically. :)

Here's a better version that will process a folder of MP3s, one-by-one, quitting when all are done;

Code: Select all

tell application "Max" to activate

tell application "Finder"
	set sourceFolder to choose folder
	
	set theFiles to (every file of sourceFolder whose name extension is "MP3") as alias list
	
	repeat with aFile in theFiles
		
		tell application "Max"
			convert aFile
		end tell
		
		delay 1
		
		tell application "System Events"
			tell process "Max"
				click button "Convert" of tool bar of window "File Conversion"
				
				set encoderOpen to true
				repeat while encoderOpen is true
					try
						window "Encoder"
					on error
						set encoderOpen to false
					end try
				end repeat
				
			end tell
		end tell
	end repeat
end tell




neil.baldwin
Posts: 23
Joined: Sun May 13, 2007 9:58 pm

Re: Help with AppleScript

Post by neil.baldwin » Tue Jul 08, 2008 8:26 am

Right, yet another version but this time to try to satisfy the request for a drag-and-drop converter.

Couple of things;

+ You can drag a file or a folder onto the script. If you drag a folder, it's contents and the contents of any subfolder will be processed.
- you need to set up your output formats manually in Max. Don't think it's possible to do this via scripting.
- there is no file-type checking so if you drag a folder onto it, ANY valid audio media will be added to the processing list
- it adds all the valid files to Max's processing list before beginning the conversion. I could have it add and convert one-by-one (as per my original need) but I think that is the lesser useful mode plus it's faster.

Try it on a couple of file first to make sure you're getting the proper result as I don't think there's a simple way to stop it once it's going. There's certainly no error checking in there :)

Instructions: open this up in Script Editor, save as an Application to your desktop, giving it a suitable name. Drag and drop files on it to get Max to convert them.

Code: Select all

on open targetFiles
	tell application "Finder"
		repeat with targetFile in targetFiles
			if (kind of targetFile is "Folder") then
				-- process folder
				processFolder(targetFile) of me
			else
				processFile(targetFile) of me
			end if
		end repeat
	end tell
	
	-- Convert the files
	
	tell application "System Events"
		tell process "Max"
			click button "Convert" of tool bar of window "File Conversion"
			set encoderActive to true
			repeat until encoderActive is false
				try
					window "Encoder"
				on error
					set encoderActive to false
				end try
			end repeat
		end tell
	end tell
end open

on processFolder(theFolder)
	tell application "Finder"
		set theFolders to every folder of theFolder
		repeat with aFolder in theFolders
			processFolder(aFolder) of me
		end repeat
		set theFiles to every file of theFolder
		repeat with aFile in theFiles
			processFile(aFile as string) of me
		end repeat
	end tell
end processFolder

on processFile(theFile)
	--display dialog (theFile as string)
	tell application "Max"
		convert theFile
	end tell
end processFile

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests