Simple AppleScript - QuickTime Player Issue – Need Help

Hi all,

I'm trying to do a very basic thing using Applescript but I'm having trouble. I am trying to create a very simple Applescript to just open a plain vanilla MP3 file on my desktop with QuickTime Player.

I want to use QuickTime and have it open so user can control it etc.

The basic problem is that the file name seems to be sometimes randomly causing problems especially when it has spaces in the name or the path folder names etc.

If I double click on any file they all open in QuickTime.

I created a very basic short audio file and saved it with a few different names like:

test 1.mp3
this is a test.mp3

Then I created this very simple Applescript below.

generally = test1.mp3 will almost always open and start playing, however the other Versions with spaces in the title often produce and error (open thesong line fails i think) like:

The document β€œtest1.mp3” could not be opened.
The file isn’t compatible with QuickTime Player.

error "QuickTime Player got an error: Can’t get document 1. Invalid index." number -1719 from document 1

  • think this is because it can't open the file.

Any help would be appreciated.

Applescript ...

tell application "Finder"
	--varied attempts like this...
	set thesong to "drive:Users:me:Desktop:music:test1.mp3" as string
	set thesong to "drive:Users:me:Desktop:music:test1.mp3"
	set thesong to "drive:Users:me:Desktop:music:this is an audio test.mp3" as string
	set thesong to "drive:Users:me:Desktop:test1.mp3" as string

end tell

tell application "QuickTime Player"
	open thesong
	play document 1
end tell

Also I tried adding delays thinking that QuickTime needed time to open before open / play song - but still have random fails...

tell application "QuickTime Player"
	delay 3
	open thesong
	delay 3
	play document 1
end tell

Hi Dealtek,

I got it working (in Script Debugger) by changing the sequence of 'open' and 'activate'.

tell application "Finder"
   set theSong to "drive:Users:me:Desktop:music:test1.mp3"
end tell

tell application "QuickTime Player"
   open theSong
   play document 1
end tell

To me it seems that the 'open dialogue' that appears when Quicktime is activated causes the error in some way.

I couldn't figure out (in the time I have) how to start Quicktime without the 'open dialogue' or how to close it.

Hope this helps,

Advice on getting advice from this forum.

If you want help to be:

  • delayed
  • half-hearted
  • completely unresponsive.

Always refer to what you are hoping for (especially a script) as "simple". Ideally "very simple".
If you can, try to use the word "simple" twice.

With any luck. you will get no help at all.

At the very least, any help will be delayed and unenthusiastic :slight_smile:

  1. If it were "simple" you would be able to do it yourself.
  2. If you can't do it yourself, then you have means of knowing whether it is simple or not.
  3. You are just announcing in advance that any time others spend on your issue will be trivial and inconsequential.

FWIW, when you read the Scripting Definition entry for an application like QuickTime, the most helpful thing to pay attention to is the type of data expected.

Here, for example, we are told that data of type file is required by open

You have tried 4 different strings.

Extending the experiment to 40, 400, or 4000 more string permutations would not have improved your chances.

What you need to Google now, is how to create a reference that fits the requirement of a file type argument.

You may learn that

(Path string) as alias

is one incantation that can do this for you .

So one possibility for the next experiment might look something like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "QuickTime Player"
    set mp3 to open ((((path to desktop folder) as string) & "test.mp3") as alias)
    play mp3
end tell

But above all, do remember to use the word simple as often as possible, to achieve maximum delay and minimum enthusiasm in any eventual responses.

This will help others to avoid wasting their time.


Hi Rob,

Thank you for teaching me some new Applescript!
If you know what to do (and you clearly know) things must be really very simple :crazy_face:

Hope you have a great weekend!

1 Like

Hey @dealtek,

Setting a string to a string is a trifle redundant – path-string or not.

set myStr to "I'm just a string..."
return class of myStr

It should also be noted that string has been deprecated in favor of text (unicode text) for a very long time now.

See the Text_Class_link in the Applescript Language Guide.

@ComplexPoint is using an alias file-reference in his example, and I actually prefer this method. Aliases go back to Apple's HFS file system and rely on HFS paths which are delimited with a colon β€˜:’ rather than a forward slash β€˜/’ like POSIX Paths.

Unfortunately Apple decided to reduce the functionality of aliases in either High Sierra or Mojave, and I reckon this has to do with changes related to the APFS (Apple File System). Aliases in AppleScript no longer keep track of items even when their name/location has changed. (Major bummer.)

Since macOS is increasingly Unix-centric, this example is going to focus on using POSIX Paths.

(I'll post something more universal later.)

NOTE – dragging a file into Apple's Script Editor app will create a POSIX Path, but if you're not paying attention it might try to open the file as text. That can be especially problematic if the file in question is a large movie or application.

After spending a few minutes testing with it today I can't believe anyone would want to write AppleScript if they had to fight with this awful app constantly. Thank goodness Script Debugger Lite is available for free and is so completely superior. (I've used the professional commercial version since 1996.)

Drag and drop in Script Debugger gives you many more options that the Script Editor:



# Auth: Christopher Stone
# dCre: 2021/02/06 09:50
# dMod: 2021/02/06 14:37
# Appl: QuickTime Player, System Events
# Task: Open a given file in the QuickTime Player.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @QuickTime_Player, @System_Events

# Fully hard-coded POSIX Path -- NOT recommended.

# set posixFilePath to "/Users/YourUserName/Desktop/TEST VID.mp4"

# Better:
# $HOME folder based or Tilde format POSIX Path β€˜~/’ --> (root user folder).

set posixFilePath to "~/Desktop/TEST VID.mp4"

# I personally prefer the following method of creating what I call an "Anchored Alias".
# This is the method @ComplexPoint illustrated.
# Fortunately I have a hotkey-driven script to create these automagically which makes using them effortless instead of tedious.
# Anchored paths are _nearly always_ better than hard-coded paths.

# set fileRefAlias to alias ((path to downloads folder as text) & "TEST VID.mp4")


set posixFileRef to make_Posix_FileRef_From_POSIX_Path(posixFilePath)

# set hfsPathStr to (posixFileRef as text)
# set posixPathStr to POSIX path of (posixFileRef as text)

tell application "QuickTime Player"
	# Starts up the app without the open file dialog.
	# Not necessary in this use-case, but often a good idea.
	if not running then launch
	# open hfsPathStr -- Works on Sierra, but Mojave doesn't like it.
	# open posixPathStr -- Works on Sierra, but Mojave doesn't like it.
	# Activate "appName" brings an application to the front, launching it if necessary.
	# A proper file-reference is needed on Mojave.
	#    - Sierra was tolerant of a path string – either HFS or POSIX.
	# Open the previously defined file-ref and assign the document-object to a variable.
	set newDoc to open posixFileRef
	# Play the newly opened document.
	play newDoc
end tell

on make_Posix_FileRef_From_POSIX_Path(posixFilePath)
	tell application "System Events"
		set fileRef to POSIX path of disk item posixFilePath
	end tell
	set posixFileRef to POSIX file fileRef
	return posixFileRef
end make_Posix_FileRef_From_POSIX_Path

When you write code you're lucky when things are simple...   :sunglasses:

Once you gain some proficiency with a given scripting/programming language many things become relative shades of easy, but few things are truly simple. Too many finicky details toss simple out with the bathwater.


1 Like

Thanks Reheer - ComplexPoint & ccstone for all your kind assistance. Sorry for the delay.

FWIW a variant for Execute JavaScript for Automation actions in KM:

(() => {
    "use strict";

    const mp3Path = "~/Desktop/testB.mp3";

    const main = () => (
        quickTime =>
        Application("QuickTime Player")

    // --------------------- GENERIC ---------------------

    // filePath :: String -> FilePath
    const filePath = s =>
        // The given file path with any tilde expanded
        // to the full user directory path.

    return main();

Thanks this will be helpful!