Identifiers and State

As well as using identifiers, there are also command to check the state of iRecordMusic, as to whether it is mute or not, whether it is playing any audio, or recording. You can also mark a running copy of iRecordMusic, to reserve it for scheduling. As there could be multiple copies of the application running, it is useful to have a routine to loop through existing copies of iRecordMusic to find one which is free (e.g. not playing anything, not marked for scheduling, or not recording), and then reserve it. Failing that, it is possible to spawn a new instance of the application.

Here is an example of finding an iRecordMusic suitable for scheduling, or launcing a new iRecordMusic if none are suitable. This is actual code from our helper AppleScripts which iCal launches.

-- Get a string rawid of a IRM application which is not scheduled and not recording, else missing value
-- If there is no suitable IRM app, a new one is launched, but we loop and check to make sure this app doesn't get grabbed by another script.
-- The key call is to 'reserve for scheduling' which should return true if you grab the app for your own use
on getFreeIRM()
	tell application "iRecordMusic"
		set theIRMid to missing value
		set found to false
		set counter to 0
		repeat
			set theList to irmIDs
			set numList to count of theList
			repeat with i from 1 to numList
				set iid to item i of theList
				if ((reserve for scheduling with iid) is true) then
					set theIRMid to iid
					set found to true
					exit repeat
				end if
			end repeat
			if found then
				exit repeat
			end if
			
			set counter to (counter + 1)
			
			if counter > 20 then
				exit repeat
			end if
			
			-- Create a new instance of IRM
			new irm
			
			tell me
				do shell script ("sleep 5")
			end tell
						
		end repeat
		return theIRMid
	end tell
end getFreeIRM