Looping a Quicktime audio

I cannot figure out how to get AppleScript to loop a Quicktime audio file. I even tried commanding a key code of option cammand L but nothing seems to work

Model: intel iMac mid 2011
AppleScript: with OS X high Sierra
Browser: Safari 605.1.15
Operating System: macOS 10.13

The following works on Catalina with an MP3. You can also use GUI scripting if that’s preferred.

tell application "QuickTime Player"
	activate
	set looping of front document to true
end tell

Did not work .

error “QuickTime Player got an error: Can’t set document 1 of document "twilight_zone.mp3" to true.” number -10006 from document 1 of document “twilight_zone.mp3”

tell application “QuickTime Player”
set theSound to open file “Macintosh_HD:Users:stanlefkowitz:Video:Test:twilight_zone.mp3”
tell theSound
– present it full screen
– present it
activate
set its presenting to false – alternative means to force full screen
set looping of front document to true
play
– tell application “System Events”
– keystroke “L” using {option down, command down}
– end tell
delay 12
end tell
quit
end tell

This is the small script I was trying to get working

Did you try my script by itself or as part of your full script? If the latter, I would not expect it to work.

You can try the following, which uses GUI scripting. It works as a toggle.

tell application "QuickTime Player" to activate

tell application "System Events" to tell process "QuickTime Player"
	click menu item "Loop" of menu "View" of menu bar 1
end tell

Your full script with a few changes works on my computer. I’m not able to test it on High Sierra.

tell application "QuickTime Player"
	activate
	set theSound to open file "Media:Music:Various:Lady Gaga - Million Reasons.mp3"
	tell theSound
		play
		set its presenting to true -- sets player to full screen
		set its looping to true
	end tell
end tell

You’re already addressing the front document with your “tell theSound”, so the looping line should just be “set looping to true”.

tell application “QuickTime Player”
set theSound to open file “Macintosh_HD:Users:stanlefkowitz:Video:Test:twilight_zone.mp3”
tell theSound
set the presenting to false
set the looping to true
play
delay 24 --Play 2x
end tell
quit
–use this to see what properties can be modified
–get the properties of theMovie
end tell

This simple script will loop an audio/video file. Problem solved

The above script will only work properly if the audio/video file has a length of 12 seconds. The following will work with an audio/video of any length:

set theAudio to "Macintosh HD:Users:Robert:Working:alarm.mp3"

tell application "QuickTime Player"
	activate
	open file theAudio
	tell front document
		set the presenting to false
		repeat 2 times
			play
			delay (duration + 2)
		end repeat
	end tell
	quit
end tell

I liked the idea of repeating a song N times as suggested by the OP.

Peavine correctly pointed out, the script contains a delay determining error, but he himself suggested the wrong script. I slightly tweaked the OP’s script for myself and other users, and saved it to my script library. Here it is:


property N : 2 -- looping the song 2 times
set audioHFS to (choose file of type "mp3") as text --  hfs path

tell application "QuickTime Player"
	activate
	tell (open file audioHFS) -- we tell to document 1
		set the presenting to false -- do not show the whole document
		set the looping to true
		play
		delay (N * duration + 1.) -- N times, then quit
	end tell
	quit
end tell

KniazidisR. I wrote my script as I did for a very specific reason.

Whenever QuickTime Player loops through an audio (a song for example), there is a delay of about 1 second at the end of each playing of the song. As a result, your script quits the QuickTime Player before the last playing of the song is complete and the greater the number of song repeats the earlier this occurs. Also, and this is just a matter of personal preference, I like having a short pause between each playing of the song.

The following is an example with a 20-second MP3 apparently included with Catalina. QuickTime Player quits with 3 seconds remaining in the last playing of the MP3. As things go, this is not catastrophic but I don’t find it to be a desirable tweak.

property N : 3
set audioHFS to "Macintosh HD:System:iOSSupport:System:Library:PrivateFrameworks:HomeUI.framework:Versions:A:Resources:alarm1-b238.mp3"

tell application "QuickTime Player"
	activate
	tell (open file audioHFS) -- we tell to document 1
		set the presenting to false -- do not show the whole document
		set the looping to true
		play
		delay (N * duration) -- N times, then quit
	end tell
	quit
end tell

Now, in contrast, test how my script works.

set playPause to 1
set songRepeats to 3
set theAudio to "Macintosh HD:System:iOSSupport:System:Library:PrivateFrameworks:HomeUI.framework:Versions:A:Resources:alarm1-b238.mp3"

tell application "QuickTime Player"
	activate
	open file theAudio
	tell front document
		set the presenting to false
		repeat songRepeats times
			play
			delay (duration + playPause)
		end repeat
	end tell
	quit
end tell

Ok, I will add 1 second pause to my, but your script doesn’t repeat the song at all in my application.

There is an error in my script which occurs when playPause is set to 0. The script works reliably for me when this value is 1, although a value of 2 or 3 may be necessary.

set playRepeat to 2
set playPause to 2 -- must be 2 or greater

set audioFile to (choose file) as text

tell application "QuickTime Player"
	activate
	open file audioFile
	tell document 1
		set presenting to false -- do not present in full screen
		repeat playRepeat times
			play
			delay (duration + playPause)
		end repeat
	end tell
	quit
end tell

The following script–which uses the same basic approach as KniazidisR’'s script–should be reliable and does not truncate the last playing of the selected media file.

set repeatLoops to 2
set mediaFile to (choose file) as text

tell application "QuickTime Player"
	activate
	tell (open file mediaFile)
		set presenting to false -- do not present in full screen
		set looping to true
		play
		delay (repeatLoops * duration)
		set looping to false 
		repeat while playing
			delay 2
		end repeat
	end tell
	quit
end tell