Play Alert Sound from Posix Path ~ [SOLVED]

I would like to play a notification sound at the end of a macro using a sound file saved in a Dropbox folder.

And since I sync Keyboard Maestro using Dropbox I would like this same alert sound to play when I run the macro on another computer.

The problem is that when I run the macro on a different computer the macro fails as it cannot find the sound file.

If I could set the path to the sound file in the Keyboard Maestro Action "Play a Sound" using a Posix Path all my computers would be able to access the same sound file - but there is no option to do this in the Keyboard Maestro Action "Play a Sound".

When setting up the Action, I can navigate to any file using "other" but the Action does not show me the path to the file, so I cannot edit the path to make it a Posix Path.

What I would like is to be able to set the path to the sound file something like this:

~/Dropbox/Library/Sound Effects/02_Alpine_Horn.aif

Thanks for any help!

Check out
https://forum.keyboardmaestro.com/t/play-sound-from-variable/13326/6https://forum.keyboardmaestro.com/t/play-sound-from-variable/13326/6

Thanks. I did read that topic but I couldn't understand how to implement it for my case. I understand that I need to run an AppleScript and it involves variables but it seemed very complex and there were lots of posts on the thread with different AppleScripts.
Apologies - I usually manage to easily adapt these solutions for my own use.

Here is an alternative
1: Download Play Sound from https://download.cnet.com/Play-Sound/3000-2139_4-25096.html
or
david.blache.net

2: Create two macro calls using Apple script , 1 to play and 1 to stop

Applescript is :-

tell application "Play Sound"
play "Macintosh HD:Users:a_jl:Desktop:Acapulco.mp3" from position 0 to position 15 at volume 222 repeat 1 with priority immediate
end tell

===================================
1: It appears that Play Sound is minimalist player (free to use)

2a: Microcosm software no longer exists
2b: Writer is at david.blache.net
3: Thanks to Dr Drang for his reference to Play Sound
4: Manual/Readme below

===================================

Play Sound
Play Anything

Version: 1.6.7

Copyright 2003-2007 David Blache, Microcosm Software.
http://MicrocosmSoftware.com/playsound/

Introduction

Play Sound is a simple, no-fuss sound player for Mac OS X. It supports QuickTime sound files (for example, AIFF or MP3 files), Classic Mac OS System 7 sound files (files with a file type of 'sfil' that contain 'snd ' resources), or any 'snd ' resources embedded into any file. You can play an unlimited number of sounds concurrently or one at a time. You can loop sounds, repeat sounds, control the volume of sounds played, pause and continue sound play, and press the Escape or Command-Period (-.) keys to abort sound play.

To use Play Sound as a simple sound file player, simply drag and drop the sound file(s) onto the Play Sound icon in the Mac OS Finder. Better yet, you can configure the Mac OS X Finder to automatically open sound files with Play Sound whenever you double-click them (see Using Play Sound As a Sound Player below for easy instructions showing how to do this).

Play Sound is fully scriptable, allowing AppleScript writers to use simple, easy-to-understand English syntax to play most QuickTime-supported sound files, Classic Mac OS System 7 sound files, and even 'snd ' resources that are embedded directly in script applets. To script Play Sound, you call it from any AppleScript script or application.

How Play Sound Is Designed

Play Sound is designed, from the ground up, to run all the time, in the background. While idle, the Play Sound application uses 0% of the CPU, and uses very little memory. Running in the background allows Play Sound to process incoming play requests with lightning speed. In fact, due to the system resources needed in Mac OS X to launch applications, launching and quitting Play Sound each time you want to play a sound places more load on your system than simply leaving Play Sound running in the background! So i suggest leaving Play Sound running.

Play Sound is an Open Scripting Architecture (OSA) scripting extension packaged as a Mac OS X scriptable faceless background application. Because it is a faceless background application, while Play Sound is running, it is not displayed in the Dock, and does not have a user interface. This type of packaging offers certain benefits over the traditional scripting addition packaging:

• Because Play Sound is not a scripting addition, it does not share the global terminology namespace with all other scripting additions. To send commands to Play Sound, commands must be enclosed in a tell block. This means that script writers do not have to worry about terminology name conflicts with other loaded scripting additions.

• Since Play Sound is an application, it can be loaded and unloaded at will. To unload Play Sound, you simply send it a quit command. In contrast, to unload standard scripting additions you must remove them from the Scripting Additions folder and restart the computer.

• Play Sound lives in its own memory space rather than taking up space in the application heap of the application that called it. The more memory left for other applications to do their jobs, the better. Also, in the unlikely event that Play Sound corrupts memory due to a bug, other applications will not be affected. In comparison, if a scripting addition corrupts memory, the corrupted memory belongs to the host application using the scripting addition, which usually means the application that called the scripting addition crashes or malfunctions.

Installing Play Sound

To install the Play Sound application, simply drag it to any location on your hard drive. The first time you call the Play Sound application in a script, Applescript may ask you to locate the application file if it is not already running. As part of installation, we suggest that you configure the Finder to use Play Sound to play all sound files (See Using Play Sound As a Sound Player below for details).

Using Play Sound As a Sound Player

You can use Play Sound as a simple sound file player without having to use or learn AppleScript. To play any sound file with Play Sound, simply drag and drop any number of sound file(s) onto the Play Sound icon in the Finder.

You can also configure the Mac OS X Finder to automatically open sound files with Play Sound whenever you double-click them. You use the Finder's Info window to associate all sound files with Play Sound, so that double-clicking sound files in the Finder automatically plays them with Play Sound. To configure the Mac OS X Finder to use Play Sound to play sound files:

  1. In the Finder, select at least one of each type of sound files you want to open in Play Sound (for example, AIFF sounds, MP3 sounds, etc.). The Finder highlights the file(s) you selected.

  2. From the Finder menu bar, select File > Get Info. The Info window appears.


3. In the Info window, select Play Sound from the Open with menu (as shown above). The Finder changes the association for the selected file(s) to Play Sound.

  1. Notice that the Change All button in the Info window is now enabled. If you wish all sound files of this type to be associated with Play Sound:

    a. Click the Change All button. A dialog box appears asking if you are sure you want to make this change.

    b. Click Continue. The Finder changes the association for all files of this type to Play Sound.

Now, whenever you double-click sounds of the specified type, Play Sound will automatically play them for you!

Scripting Play Sound

You can script Play Sound with any Open Scripting Architecture client, such as Apple's Script Editor. To view the Play Sound dictionary, drag Play Sound's application icon over a script editor.

The Play Sound dictionary suite looks like this:

Class application: the Play Sound application
Properties:
player state application -- the current state of the player (playing, idle, paused, quitting)

pause: pauses the currently-playing sound
pause

play: plays a specified sound resource or file
play anything -- sound file as name, path, or alias (The file parameter is optional.)
[resource index integer] -- the ‘snd ‘ resource index
[resource ID integer] -- the ‘snd ‘ resource ID
[resource named string] -- the ‘snd ‘ resource name
[at volume integer] -- an integer value from 1 to 255 representing the volume at which to play the sound
[repeat integer] -- repeat the sound this many times
[with priority constant] -- set the priority of this sound (normal, high, immediate)
[looping boolean] -- repeat until the “stop” command is received
[concurrence boolean] -- play while other sounds are playing
[from position real] -- start playing this number of seconds from the beginning of the sound
[to position real] -- stop playing this number of seconds from the beginning of the sound

resume: resumes playing the currently-paused sound
resume

stop: stops the current sound
stop

Commands intended for Play Sound processing must be enclosed in a tell block like so:

tell application "Play Sound"
play "ChuToy"
quit
end tell

Playing Mac OS Sound Files

You can play many different Mac OS sound file formats by issuing the play command followed by the path, file reference, or alias to the sound file.

AIFF sound files are the preferred format on the Mac OS X version of the Mac OS operating system. AIFF sound files have a file type of "AIFF" or "AIFC" and contain AIFF sound information in the data fork of the file. You can also play many other sound files supported by QuickTime. In general, if you can play a sound file with QuickTime Player application, you should also be able to play it with Play Sound.

Example:

tell application "Play Sound" to play "Mac HD:Desktop Folder:Moof.aif"

Note: Although Play Sound can play an unlimited number sounds at once, the default behavior of Play Sound is to play sounds synchronously, or one at a time. This means that the current sound must finish playing before another sound will play. For more information, refer to the with concurrence command parameter, explained later in this document.

Note: If you specify a file path that is longer than 255 characters, convert the path to an alias before sending it to Play Sound, like so:

tell application "Play Sound" to play ("Really:Long:Path:Moof.aif" as alias)

Playing Classic Mac OS Sound Resources

You can play Classic Mac OS sound ('snd ') resources with Play Sound. Classic Mac OS system sound files are the preferred format on the classic version of the Mac OS operating system (Mac OS 9 and earlier). System sound files (also known as "System 7 sound files") have a file type of "sfil" and contain a "snd " resource. System 7 sounds are stored in the System suitcase in Mac OS 9 and earlier. Mac OS X currently has no System suitcase, and no equivalent of System 7 sound resources. Instead, Mac OS X stores system sounds as AIFF sound files in one of the various Library/Sounds/ folders. See "Playing Mac OS Sound Files" for information on how to play these sound files. You can play system sound files by using different combinations of Play Sound parameters as shown below.

Examples:

tell application "Play Sound"
play "Mac HD:Desktop Folder:My Sounds.rsrc" resource named "Boohoo!"
play "Mac HD:Desktop Folder:My Sounds.rsrc" resource ID 128
play "Mac HD:Desktop Folder:My Sounds.rsrc" resource index 1
end tell

Note: System 7 sounds are stored in the System suitcase in Mac OS 9 and earlier. Mac OS X currently has no System suitcase, and no equivalent of System 7 sound resources. Instead, Mac OS X stores system sounds as AIFF sound files in one of the various Library/Sounds/ folders. See "Playing Mac OS Sound Files" for information on how to play these sound files.

Note: If a System sound file contains more than one sound resource, and you do not specify the resource name, index, or ID, Play Sound plays the first sound resource in the file.

Checking the Player State

You can check the state of the player at any time. You can use this information to have your scripts do different things based on Play Sound's current state. You check the state by reading the Play Sound player state application property. This property returns one of these constants: idle, quitting, playing, or paused.

Note: Do not enclose the idle, quitting, playing, and paused constants in double quotes (").

Examples:

You can copy the current value of the player state property to a variable:

tell application "Play Sound"
set myVar to the player state
end tell

Or you can have your script evaluate the property inline:

tell application "Play Sound"
if the player state is playing then
beep
end if
end tell

Pausing and Resuming Sound Play

You can pause and resume sound play at any time by sending Play Sound the pause and resume commands.

Example:

This script causes Play Sound to pause sound play for two seconds, and then resume playing:

tell application "Play Sound"
if the player state is playing then
-- pause for two seconds
pause
delay 2
resume
end if
end tell

Stopping Sound Play

You can abort sound play immediately at any time one of two ways:

• by pressing the Escape or Command-Period (-.) keys on your keyboard
• by sending Play Sound the stop command

Example:

This script causes Play Sound to abort all currently-playing sounds:

tell application "Play Sound"
stop
end tell

Note: Using either method to stop sound play will clear all queued sounds from the queue.

Playing Concurrent Sounds

Although Play Sound can play an unlimited number of sounds at the same time (concurrently), the default behavior of Play Sound is to play sounds one at a time.

In the default mode, the current sound must finish playing before another sound beings playing. Play Sound automatically queues successive sound play requests while the current sound is playing, then plays them in the order of the received requests.

You can instruct Play Sound to play sounds at the same time by using the with concurrence optional parameter. For example,this script plays two sounds at the same time:

tell application "Play Sound"
play "Background Music.mp3"
play "Hello There.aif" with concurrence
end tell

Playing Sounds With Priority

You can instruct Play Sound to play sounds with different priorities by supplying the with priority optional parameter with the play command. There are three supported priority level constants: normal, high, and immediate. Here are descriptions of each priority level:

normal (Default) The specified sound is added to the end of the queue, to be played after all other queued sounds.
high The specified sound is added to the beginning of the queue, shifting all queued sounds down in the queue.
immediate Any currently-playing sound is aborted, and the specified sound is played immediately.

Note: Do not enclose the normal, high, and immediate constants in double quotes (").

Note: If you do not specify a priority for a sound, Play Sound plays the sound with the normal priority.

Example:

This script causes Play Sound to abort the “Test” sound and play the “Moof.aif” sound file immediately:

tell application "Play Sound"
play "Mac HD:Desktop Folder:Test"
delay 1
play "Mac HD:Desktop Folder:Moof.aif" with priority immediate
end tell

Controlling Sound Play Volume

You can instruct Play Sound to play sounds at specific volume levels by using the optional at volume parameter, supplying an integer value in the range of 0 (muted) to 255 (full volume).

Note: The behavior of this volume is exactly the same as the behavior of the standard volume slider widget you see in any QuickTime movie player. It controls the volume of the movie (sound) itself — not the overall volume of the entire system. In other words, this volume control is relative to the overall system volume.

Note: Starting with QuickTime 1.6, QuickTime supports over-amplication of sounds past full volume. While you may supply volume values higher than 255 to achieve louder sound play, distortion and noise may result.

Example:

This script plays “Moof.aif” sound file at half volume:

tell application "Play Sound"
play "Mac HD:Desktop Folder:Moof.aif" at volume 128
end tell

Looping Sounds

You can instruct Play Sound to loop sounds over and over (until a stop or quit command is received).

Example:

This script loops the “ChuToy” system sound for 5 seconds:

tell application "Play Sound"
play resource named "ChuToy" with looping
delay 5
stop
end tell

Repeating Sounds

You can instruct Play Sound to repeat sounds a specified number of times. Using this method is a more efficient way to repeat a sound (as opposed to having your script issue multiple play commands for the same sound).

Note: Specify the number of additional times you want to play the sound.

Example:

This script repeats the “ChuToy” system sound a total of six times (plays the sound once, then repeats it 5 more times):

tell application "Play Sound"
play resource named "ChuToy" repeat 5
end tell

Playing Parts Of Sounds

You can instruct Play Sound to play portions of sounds, rather than playing the entire sound, by using the from position and to position optional parameters. These parameters specify the number of seconds from the beginning of the sound to start and stop playing.

You do not need to use both the from position and to position parameters at the same time if you do not want to. If you specify only from position, the sound will play from the specified position to the end. If you specify only to position, the sound will play from the beginning to the specified position.

TIP: You can view the duration of any sound file by opening the sound file in QuickTime Player, and selecting Window > Show Movie Info from the QuickTime Player menu bar. The Movie Info window (shown below) displays the duration of the sound.


Note: To specify fractions of a second, you may use real numbers for both of these parameters.

Note: If you specify a value that is out of range for one of these parameters, Play Sound will automatically set the value to one that is in range. For example, for the sound pictured in the Movie Info window above, if you specify from position -5, Play Sound sets the value to 0 (the beginning of the sound). If you specify to position 350, Play Sound sets the value to 320 (a duration of 5 minutes, 20 seconds is a total of 320 seconds).

Example:

This script plays the portion of the sound from two seconds from the beginning to four and one-half seconds from the beginning:

tell application "Play Sound"
play "Mac HD:Desktop Folder:Test" from position 2.125 to position 4.5
end tell

Change Log

1.6.7 - Bug Fixes

  • Fixed a bug where Play Sound, on Mac OS X 10.5, was not handling sound file paths sent as Unicode text strings.

  • Fixed a bug where Play Sound would return a "player status" of "quitting" if you sent a quit command, but sounds were still playing. Now, Play Sound will return "playing" until all sounds are done playing, and then return "quitting" just before quit.

1.6.6 - Bug Fixes

  • Fixed a bug in "from position" and "to position" parameter handling that caused the specified values to be read incorrectly, resulting in Play Sound playing the wrong portions of sounds, or not playing the sounds at all when these parameters were specified.

  • Fixed a bug that caused Play Sound to generate a "parameter error" if the "from position" parameters was specified but the "to position" parameters was not specified.

1.6.5 - Added 10.2.8 Compatibility

  • Recompiled with Mac OS X 10.4u (Intel) SDK and Mac OS X 10.2.8 (PowerPC) SDK so that the Play Sound universal binary now runs in Mac OS X 10.2.8 or later on PowerPC and Intel Macs.

1.6.4 - Bug Fix

  • Fixed a bug where Play Sound would return the bogus error "Could not get the next document in the list." when Play Sound was called via AppleScript.

1.6.3 - Mac Intel Support, Bug Fixes

  • Play Sound is now a Universal application, and runs on both PowerPC™- and Intel™-based Macs.
  • Play Sound was not properly setting the playback volume for concurrent sounds. This is now fixed.

1.6.2 - New Functionality

  • Added the ability to send the play command with no parameters to resume paused sounds.

  • Added the pause and resume commands, to conform better to Apple's scripting interface guidelines. These commands let you pause and resume sound play. You should use these commands rather than setting the player state property from now on. While Play Sound currently supports the setting of the player state to paused and playing, those methods are deprecated and should not be used!

1.6.1 - New Functionality

  • Added the from position and to position optional parameters. These parameters let you play portions of a sound by specifying the number of seconds from the beginning of the sound to start / end playing. See the documentation for details.

  • Fixed a bug that caused the player state property to be tracked and reported incorrectly.

1.6 - New Functionality, Bug Fixes

  • Play Sound is now a Mach-O application that does not run in Classic Mac OS 9. (For Classic Mac OS 9 support, use Play Sound version 1.5.3.)

  • Due to changes in the dictionary, you will need to recompile existing scripts to use them with this version of Play Sound. My apologies for this inconvenience — I will refrain from making this sort of change again in the future.

  • Removed the LSPrefersCarbon property from the application bundle property list. (Due to a bug in Mac OS X 10.4 and 10.4.1, the existence of this property causes the application icon to appear in the Dock when the application is launched via AppleScript. I have made Apple aware of this problem — Problem ID 4126824: "LSUIElement property is not honored for some apps launched via AppleScript")

  • Because this is now a Mach-O application, both LSBackgroundOnly and LSUIElement properties are included in the application bundle property list.

  • Play Sound no longer plays the video tracks of QuickTime movies. Only audio tracks are played.

  • Added the player state application property, along with associated state constants: playing, paused, idle, and quitting. See the documentation for details.

1.5.3 - New Functionality

  • Added a new optional with priority parameter that lets you specify when a sound should play. Supported priorities are: normal, high, and immediate. See the documentation for details.

1.5.2 - Bug Fix

  • I inadvertently changed part of Play Sound's dictionary back in version 1.5 that caused scripts to break. Recompile and problem scripts with this new version of Play Sound to fix the problem.

1.5.1 - Bug Fix

  • Play Sound, under certain circumstances would fail to close a sound file if the system call opening the sound file generated an error but still opened the file. Play Sound now unconditionally closes sound files immediately after reading them. This should eradicate all "File xxx is open by an application" errors.

1.5 - New Functionality

  • Play Sound now allows you to optionally specify the volume at which to play sounds. Use the at volume parameter to specify an integer value in the range 0 (silent) to 255 (full volume) representing the volume at which Play Sound should play the sound.

Note: Starting with QuickTime 1.6, QuickTime supports over-amplication of sounds past full volume. While you may supply volume values higher than 255 to achieve louder sound play, distortion and noise may result.

1.4.3 - Bug Fix

  • Script Editor in Mac OS X 10.3 refused to display the Play Sound dictionary, because the Play Sound package Info.plist file did not have a NSAppleScriptEnabled property. This version has this property, and therefore displays its dictionary in Mac OS X 10.3's Script Editor.

1.4.2 - Bug Fix

  • Haste truly makes waste! : / In my hurry to get 1.4.1 out of the door, I inadvertently screwed up code that checks for quit events. As a result, Play Sound 1.4.1 quit immediately upon receiving a quit event, regardless of whether sounds were queued or playing. This version only quits once all sounds are finished playing.

Note: To abort all sound play and quit immediately, you can send Play Sound a stop command immediately followed by a quit command.

1.4.1 - Bug Fixes, New Functionality

  • Play Sound 1.4 was crashing silently on quit, causing debug information to accumulate in the console log. This is now fixed. Special thanks to Dan Sanfilippo for bringing this problem to my attention!

  • In the interest of making Play Sound more responsive to commands, I have decreased the amount of time it takes Play Sound to respond to events from every half second to every 60th of a second. This increases Play Sound's responsiveness significantly. The drawback to this is that Play Sound will take slightly more CPU time during sound play than before. Note: If this turns out to be a problem for anyone, I can always change it later; so please let me know.

  • Play Sound can now handle a virtually unlimited number of simultaneous sounds playing at once. Previous versions were limited to 30 concurrent sounds maximum. This version is limited only by memory available and hardware limits on the host system. = )

  • Play Sound now uses a much, much more efficient method to keep track of sounds that are being played simultaneously. This results in much, much more efficient memory usage. ; )

1.4 - Bug Fixes, New Functionality

  • The synchronous sound file play code introduced in version 1.1 of Play Sound was a quick hack, and had many problems as a result. For instance, if you played more than one synchronous sound, Play Sound would quit responding to any other events (such as quit events or other play events). In this version, Play Sound's play event handling has been completely rewritten. Play Sound now maintains a queue of play commands, pushing each sound onto the queue, and playing sounds in the order of the play requests.

1.3.1 - Bug Fixes

  • Play Sound left some sound files open after playing them. This could cause problems when those files were accessed later on. Symptoms of this problem include: if you try to unmount a volume containing a sound file that was left open, or empty the trash with sound file that was left open in it, the system complains that the file is still in use by an application. Play Sound now closes files as soon as possible after opening them. (Many thanks to "Skeeve" on Version Tracker for notifying me of this bug!)

  • Play Sound had a serious memory leak where sound file data (movies) would stay in memory after being played. Memory is now disposed of properly.

1.3 - New Feature, Bug Fixes

  • Play Sound now allows you to press the Escape key or the Command-period (-.) key combination to abort the currently-playing sound. Play Sound's event loop timer (which services currently-playing QuickTime movies and examines the command-period and escape keys) executes every half-a-second. This, hopefully, provides the best balance of responsiveness and performance.

  • Some example scripts used "Play Sound Debug", a debug version of Play Sound used during development. This caused AppleScript to display a dialog box asking you to locate Play Sound Debug when you ran the scripts. These scripts have been properly recompiled.

1.2.2 - Compatibility Update

  • Added the following key/value pair for each supported sound file type to the "Info.plist" file for better compatibility with the Mac OS X Finder:

    key "CFBundleTypeRole" value string "Viewer"

1.2.1 - Bug fix

  • In my haste to release Play Sound 1.2, I accidentally left out changes to the "Info.plist" file that would allow the Finder to associate popular sound file types with Play Sound. This release includes those changes. Please accept my apologies if this caused anyone inconvenience!

1.2 - New Features

  • Play Sound now handles sound files dragged on top of the Play Sound icon or sound files double-clicked in the Finder.

1.1 - New and Improved Features

  • Play Sound now uses QuickTime to play all sound types (including 'snd ' resources). Play Sound converts 'snd ' resources to QuickTime movies internally before playing them. This allowed me to completely eradicate all old Carbon Sound Manager code from the application - goodbye and good riddance! A neat side effect of this is that Play Sound's already small size was reduced to a mere 96k. = )

  • Play Sound now plays all sounds synchronously, by default. This means it is no longer necessary to delay scripts to wait for sounds to finish playing.

  • Added a new, optional with concurrence parameter that lets you play sounds concurrently, while other sounds are playing. See the documentation for details.

  • Added a new, optional repeat parameter that lets you specify a number of times to repeat a sound. See the documentation for details.

  • The application is now a bundled Mac OS X application.

  • Play Sound is now distributed in a disk image.

  • Added a new and improved Mac OS X icon.

  • Added to and improved existing example scripts.

1.0.8 - Bug Fix

  • Play Sound is now able to play files specified as textual paths in Mac OS X.

1.0.7 - New functionality

  • Play Sound now loops Mac OS (QuickTime) sound files as well as Mac OS sound ('snd ') resources.

1.0.6 - New functionality

  • Play Sound now waits for all currently-playing sounds to finish playing before responding to the quit command. This means it is no longer necessary to place delay commands in scripts before telling Play Sound to quit.

1.0.5 - New functionality

  • Added a looping parameter and matching stop parameter. You can now instruct Play Sound to loop sounds indefinitely until you issue the stop command.
  • Added the resource index, resource ID, and resource name parameters.
  • Rewrote QuickTime sound play functions - QuickTime sound play is now less CPU intensive.

1.0.4 - New feature

  • Play Sound now uses QuickTime to play AIFF sound files. As a side effect, you can now have Play Sound play any sound file that QuickTime recognizes.

1.0.3 - New feature

  • Play Sound now adds the calling application to the resource search path. This means that you can embed 'snd ' resources in your AppleScript application and use Play Sound to play the sounds.

1.0.2 - Changes in error handling

  • Changed the error handling. Removed code that returns a reply for errors. All errors are now returned using the default AppleEvent error handling mechanism. This means that a script editor will show an error dialog unless you wrap the play sound command in a try block.

1.0.1 - Bug fixes

  • Fixed a bug that caused Play Sound to return the error message "A fatal error occurred: 0" when there was no error.

  • Added typeFSS and typeObjectSpecifier to supported types. You can now enclose the play sound file path in parenthesis without generating an error.

1.0 - Initial Release

Another approach would be to edit the sound-file path in a macro like this:

(change the path value for the KM variable fpSoundPath to refer to some other sound file)

Play sound from path.kmmacros (18.8 KB)

And this version of the code would also expand the posix tilde:

(() => {
    'use strict';
    const main = () => {
        const kme = Application('Keyboard Maestro Engine');

        kme.doScript(`<dict>
            <key>DeviceID</key>
            <string>SOUNDEFFECTS</string>
            <key>MacroActionType</key>
            <string>PlaySound</string>
            <key>Path</key>
            <string>${filePath(kme.getvariable('fpSoundPath'))}</string>
            <key>TimeOutAbortsMacro</key>
            <true/>
        </dict>`);
    };

    // filePath :: String -> FilePath
    const filePath = s =>
        // The given file path with any tilde expanded
        // to the full user directory path.
        ObjC.unwrap(ObjC.wrap(s)
            .stringByStandardizingPath);

    return main();
})();

The easiest AppleScript for this would seem to be this one:

tell application "Keyboard Maestro Engine" to play sound "/Users/USERNAME/Dropbox/Library/Sound Effects/02_Alpine_Horn.aif"

It doesn't look like you can use a tilde abbreviated path for it, but as long as you use a full path like in that sample script (replacing USERNAME with your actual user name, of course) it otherwise seems fairly straightforward.

1 Like

and this version of @gglick's solution (with your own file path in lieu of that below) would add tilde expansion:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on run
    tell application "Keyboard Maestro Engine"
        play sound my filePath("/System/Library/Sounds/Glass.aiff")
    end tell
end run


-- filePath :: String -> FilePath
on filePath(s)
    ((current application's ¬
        NSString's stringWithString:s)'s ¬
        stringByStandardizingPath()) as string
end filePath
1 Like

Thank you everyone! This has been annoying me for a while (that I can't choose an alert sound outside of the standard Mac ones and have it available across all my computers).

I went with the solution suggested by gglick and refined by ComplexPoint to accept the ~

What I like is that there is only one Action involved so very easy to copy into other macros.

Thanks to everyone again.

For completeness here is my Macro incorporating the solution (or rather the single Action in a Macro)

Thanks again everyone - this is an amazing forumPlay Sound from Dropbox with Tilde in Path Example.kmmacros|attachment (1.9 KB)

Play Sound from Dropbox with Tilde in Path Example.kmmacros (1.9 KB)

OK it took me a little while but here is an alternative solution.

set fullpath_s to POSIX file ((system attribute "HOME") & "/Desktop/Acapulco.mp3") -- i.e. ~/Desktop....

Play sound - using ~: in path Acapulco .kmmacros (2.6 KB)

OK and for those who want the fewest lines possible, here is the minimalist version using the KM Internal play sound action. Note you cannot stop it other than cancelling the macro, so best to reduce the duration of the mp3 file.

and you may need this cancel all macros

image

Play sound - using ~: in path Acapulco using KM internal play sound.kmmacros (2.2 KB)

And thank you so much, jonathonl, for all the time you put into this!

In my case, as I'm using this for playing a short alert sound at the completion of Macros, it is no problem that the sound has to play until finished.

The combination of Keyboard Maestro and AppleScript is so powerful :slight_smile:

My pleasure. I decided to bite the bullet and learn applescript.. to solve it.
As JMichaelTx says Script Debugger 7 is essential... mine is still on trial.