How to close Music using AppleScript?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

How to close Music using AppleScript? Sun Nov 03, 2024 1:22 am • by pumpkinman
Am I using the embedded script action correctly? How do I close an application after importing? I am using the "Import file into Music" action. This leaves the Music app open after importing so I want to close it. I use this code:

Code: Select all
if application "Music" is running then
   delay 3.0
   tell application "Music" to quit
end if


When I run it in Script Editor, it does exactly what I want (I delay for 3 seconds in case it takes longer for the import to happen. Just a hacky failsafe.) When I run it with Hazel I see this in the log (and Music doesn't close):

Code: Select all
2024-11-03 09:30:56.943 hazelworker[1763] Sixpence None the Richer: Rule Run on sub-folders matched.
2024-11-03 09:30:56.948 hazelworker[1763] 13 There She Goes.mp3: Rule Import new music matched.
2024-11-03 09:30:57.145 hazelworker[1763] /Users/fuser/Music/Media.localized/Sixpence None The Richer/Sixpence None the Richer/13 There She Goes.mp3 added to Music playlist Library
2024-11-03 09:30:57.225 hazelworker[1763] [Error] AppleScript failed: Error executing AppleScript on file /Users/fuser/Music/Media.localized/Sixpence None The Richer/Sixpence None the Richer/13 There She Goes.mp3.
2024-11-03 09:30:57.226 hazelworker[1763] OSAScript error: {
    NSLocalizedDescription = "Music got an error: Can\U2019t get \U00abclass RUNG\U00bb.";
    NSLocalizedFailureReason = "Can\U2019t get \U00abclass RUNG\U00bb.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,69069 \"Music\"]>";
    OSAScriptErrorAppNameKey = Music;
    OSAScriptErrorBriefMessageKey = "Can\U2019t get \U00abclass RUNG\U00bb.";
    OSAScriptErrorMessageKey = "Music got an error: Can\U2019t get \U00abclass RUNG\U00bb.";
    OSAScriptErrorNumberKey = "-1728";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'obj '{ 'form':'prop', 'want':'prop', 'seld':'RUNG', 'from':null() }>";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2024-11-03 09:30:57.229 hazelworker[1763] 04 Kiss Me.mp3: Rule Import new music matched.
2024-11-03 09:30:57.262 hazelworker[1763] /Users/fuser/Music/Media.localized/Sixpence None The Richer/Sixpence None the Richer/04 Kiss Me.mp3 added to Music playlist Library
2024-11-03 09:30:57.269 hazelworker[1763] [Error] AppleScript failed: Error executing AppleScript on file /Users/fuser/Music/Media.localized/Sixpence None The Richer/Sixpence None the Richer/04 Kiss Me.mp3.
2024-11-03 09:30:57.270 hazelworker[1763] OSAScript error: {
    NSLocalizedDescription = "Music got an error: Can\U2019t get \U00abclass RUNG\U00bb.";
    NSLocalizedFailureReason = "Can\U2019t get \U00abclass RUNG\U00bb.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,69069 \"Music\"]>";
    OSAScriptErrorAppNameKey = Music;
    OSAScriptErrorBriefMessageKey = "Can\U2019t get \U00abclass RUNG\U00bb.";
    OSAScriptErrorMessageKey = "Music got an error: Can\U2019t get \U00abclass RUNG\U00bb.";
    OSAScriptErrorNumberKey = "-1728";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'obj '{ 'form':'prop', 'want':'prop', 'seld':'RUNG', 'from':null() }>";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}


Thanks!
pumpkinman
 
Posts: 3
Joined: Sat Nov 02, 2024 10:59 am

Re: How to close Music using AppleScript? Mon Nov 04, 2024 11:02 am • by Mr_Noodle
I believe you emailed about this but I'll answer here so ignore the email.

It doesn't look like "running" is in the AppleScript dictionary. Maybe it's some sort of extension. Here's a link to a way to tell if an application is running which is more standard: https://discussions.apple.com/thread/67 ... ortBy=rank
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: How to close Music using AppleScript? Tue Nov 05, 2024 8:56 am • by pumpkinman
Thanks! I just removed the "if" conditional and told AppleScript to just close the app:
Code: Select all
delay 3.0
tell application "Music" to quit
pumpkinman
 
Posts: 3
Joined: Sat Nov 02, 2024 10:59 am


Return to Support