0

Question:

I'm trying to use AppleScript to automatically set the start and stop times for selected tracks in the macOS Music app. While the start time is correctly set to 10 seconds, the stop time (finish) does not get applied.

Here’s the code I’m using:

tell application "Music"
    set selectedTracks to selection
    repeat with aTrack in selectedTracks
        set trackDuration to (duration of aTrack) / 1000 -- Duration in seconds
        set start of aTrack to 10.0 -- Set start time to 10 seconds
        set finish of aTrack to (trackDuration - 10.0) -- Set stop time 10 seconds before the end
    end repeat
end tell

The issue is that the start time gets set correctly, but the stop time does not change and the "Stop" checkbox is not checked in the Options tab of the track info. How can I correctly set the stop time and ensure the checkbox is activated?

Any help or advice would be appreciated!

I am a beginner und have used ChatGPT. I work on macOS 15.0 Sequoia Beta, Music 1.5.0.174

4
  • Why is 'trackDuration' — ie the duration — divided by 1000? Your track would have to be hours in length for the 'finish' to work.
    – Mockman
    Commented Sep 6 at 19:05
  • according to chatGPT: The trackDuration in the Music app’s AppleScript is returned in milliseconds, so dividing it by 1000 converts the value into seconds. For example: • If the track duration is 180000 milliseconds (3 minutes), dividing by 1000 gives you 180 seconds, which is easier to work with when setting start and stop times. This conversion ensures you’re working in seconds rather than milliseconds, making it more intuitive to set specific start or stop points in the track. And it also seems plausible, because the start and stop time can be set in 0.01 second increments.
    – Zuano
    Commented Sep 7 at 8:03
  • 1
    Well, ChatGPT is not a reliable source for anything. Did you check the Music dictionary in Script Editor? There is not even a property called trackDuration, it's called duration (which is what your script uses, but wrongly).
    – nohillside
    Commented Sep 7 at 10:23
  • Whatever else there is to say about chatgpt, I don't think people should take anything it produces literally. That's not what it's meant for. It will happily make up anything. For this particular case, as nohillside suggests, you can look duration up for yourself in the applescript dictionary. Anyway, trackDuration in your script is actually a variable… which you are setting with the set command. You could call it anything and the result would still be the same. Think seconds.
    – Mockman
    Commented Sep 7 at 14:50

1 Answer 1

1

Solution:

Thank you for the feedback, and you were right—there was no need to divide the duration by 1000. The duration property in AppleScript for the Music app returns the time in seconds, not milliseconds.

Here is the updated and working AppleScript:

tell application "Music"
    set selectedTracks to selection
    repeat with aTrack in selectedTracks
        -- Get the duration of the track (in seconds, no need to divide by 1000)
        set trackDuration to duration of aTrack
        
        -- Set the start time to 10 seconds
        set start of aTrack to 10
        
        -- Set the stop time 10 seconds before the end
        set finish of aTrack to (trackDuration - 10)
    end repeat
end tell

Changes:

  • Removed the division by 1000, as the duration is already returned in seconds.
  • The start and finish times are set directly using seconds.

This script works perfectly now! Thanks again for pointing out the mistake.


1
  • You should accept your answer, in case anyone else has a similar issue.
    – Mockman
    Commented Sep 8 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .