Limit ringtone length AppleScript - applescript

I have the AppleScript code shown below which tells iTunes to convert the track from the selection. I was wondering how I would limit the length of the track that will be converted?
tell application "iTunes"
set theFiles to the selection
repeat with theTrack in theFiles
with timeout of 120 seconds
set theSecondTrack to first item of (convert theTrack)

If you wanted to to limit the length of the converted track via the iTunes GUI, you would set the "Stop Time" of the original track in Get Info>Options. The corresponding AppleScript property for this is finish (of the track class).
So the steps in your repeat loop should be:
Get the original stop time of the track (usually this will just be the full duration of the track)
Set the stop time to your limit length (in seconds)
Convert the track
Set the stop time back to what it was at 1.
Example 60-sec limit:
repeat with theTrack in theFiles
tell theTrack
set originalFin to finish
set finish to 60
-- Track conversion code goes here
set finish to originalFin
end tell
end repeat

Related

Using converted tracks in iTunes applescript

I have a script that iterates through tracks in a playlist. If they are already mp3 it adds them to my iPod (duplicate currentTrack to ipod_lib) and removes them from the playlist - this part works fine. If they are lossless however, I want to convert them to mp3 and add them to my iPod instead of add the lossless files themselves since they are too large. The line duplicate convertedTrack to ipod_lib throws the following error...
error "iTunes got an error: Can’t set library playlist id 270897 of source id 235166 to {file track id 322339 of library playlist id 38702 of source id 68}." number -10006 from library playlist id 270897 of source id 235166
the locateiPods code is pulled from a script on http://dougscripts.com/
Really not sure what I'm doing wrong here...
on locateiPods()
set the volumes_directory to "/Volumes/" as POSIX file as alias
set the volume_names to list folder volumes_directory without invisibles
set mounted_iPods to {}
repeat with i from 1 to the count of volume_names
try
set this_name to item i of volume_names
set this_disk to ("/Volumes/" & this_name & "/") as POSIX file as alias
set these_items to list folder this_disk
if "iPod_Control" is in these_items then
set the end of the mounted_iPods to this_disk
end if
end try
end repeat
-- check for iPod count
if the mounted_iPods is {} then
--
try
display dialog "iPod is not mounted." buttons {"Cancel"} with icon 0 giving up after 15
on error
error number -128
end try
else if the (count of the mounted_iPods) is greater than 1 then
-- choose iPod
set the ipod_names to {}
repeat with i from 1 to the count of the mounted_iPods
set this_iPod to item i of the mounted_iPods
tell application "Finder"
set the end of the ipod_names to the name of this_iPod
end tell
end repeat
tell application "iTunes"
activate
set this_name to (choose from list ipod_names with prompt "Pick the iPod to use:") as string
end tell
if this_name is "false" then error number -128
repeat with i from 1 to the count of the ipod_names
if item i of the ipod_names is this_name then
set this_iPod to item i of the mounted_iPods
exit repeat
end if
end repeat
else
set this_iPod to item 1 of the mounted_iPods
end if
return this_iPod
end locateiPods
tell application "iTunes"
set allTracks to every track in user playlist "TEST"
set the_iPod to my locateiPods() -- this is a path
set the_iPod_name to text 1 thru -2 of (the_iPod as string)
set ipod_src to some source whose name is the_iPod_name
set ipod_lib to library playlist 1 of ipod_src
repeat with i from 1 to count of allTracks
set currentTrack to item i of allTracks
set fileType to kind of currentTrack as string
if fileType is "MPEG audio file" then
duplicate currentTrack to ipod_lib
tell playlist "TEST" to delete contents of currentTrack
end if
if fileType is "Apple Lossless audio file" then
set convertedTrack to convert currentTrack
duplicate convertedTrack to ipod_lib
end if
end repeat
end tell
It seems doing the following makes it work, which suggests that convertedTrack is not of type track???
set convertedTrack to convert currentTrack
repeat with theTrack in convertedTrack
duplicate theTrack to ipod_lib
end repeat
I always used "convert" for list of tracks and then result is list of tracks. by the way it is much faster to convert list of tracks instead of doing loop to convert each track. May be you should do first a select of all tracks whose kind is lossless.
About convert, also keep in mind that convert does the conversion to the format set in your iTunes preferences. Then your scrip will depend of these preferences. I suggest, to be more safe, to save current preference, set the convert mode to format you want MP3, and then reset preferences to what they were before. You can use bellow script:
-- get & save current encoder
tell application "iTunes" to set Encoder_Base to name of current encoder
-- change encoder to MP3
tell application "iTunes" to set current encoder to (get first encoder whose name contains "MP3")
Also, may be you can optimise your iPod subroutine using Source method instead of looking for volumes mounted. iTunes kind of source provides an efficient way:
-- kind of source could be : library, iPod, audio CD, MP3 CD, radio tuner, shared library, iTunes Store
tell application "iTunes"
set myList to every source whose kind is iPod
if myList is {} then
display alert "no ipod connected"
return
else -- I assume if one iPod found, there is only 1 connected !!
set SourceName to name of first item of myList
set SourceID to id of first item of myList
end if
end tell
I hope it helps.

In Applescript, Retrive playlist information from Spotify, and enable/disable shuffling

I know that there are many commands in AppleScript for Spotify such as the simple playpause command, but how would I pull a playlist's information from Spotify, and paste it in a choose from list? I would like it to take all of the songs from wherever you're listening from, and paste them in a choose from list so you can choose which song you would like to listen to. Is this even possible? Can I do something similar?
Also, how would you enable/disable shuffling?
Additionally, is there a way to search Spotify through AppleScript?
I'm not sure if any of these are possible, and Google doesn't have any info on this right now. Does anyone know how to do this?
The Spotify AppleScript implementation does not have specific commands to get at playlist information -- it only exposes "current track" to get info about the currently-playing track and "next track" to play the next track. You can work around this limitation to build an AppleScript array containing all the tracks in a current playlist.
set trackNameList to {}
set trackIDList to {}
tell application "Spotify"
activate
set shuffling to false
set repeating to true
set sound volume to 0
if player state is not playing then
playpause
end if
set trackID to spotify url of current track
repeat while trackIDList does not contain trackID
set end of trackIDList to trackID
set end of trackNameList to name of current track
next track
delay 1 -- otherwise Spotify misbehaves
set trackID to spotify url of current track
end repeat
end tell
tell me to activate
set chosenNames to choose from list trackNameList without multiple selections allowed
set chosenName to (item 1 of chosenNames) as string
repeat with i from 1 to count of trackNameList
set itsName to (item i of trackNameList) as string
if itsName is chosenName then
exit repeat
end if
end repeat
set trackID to (item i of trackIDList) as string
tell application "Spotify"
activate
set sound volume to 100
play track trackID
end tell
There is no AppleScript support for searching in Spotify.

Applescript quicklook: arrow key down 1000 times

I'm a total beginner in apple script.
I would like to open a folder directory that contains hundreds of photos and to view each photo 1 second with quick look and then go to the next (suppose to use down arrow / key code '124').
I don't have any idea of how to build the script. I tried to compile some formulas from other questions or use the manual rec but it doesn't work.
Thanks!
Try following script. I made the delay longer so you have time to stop the script (to test it):
set timePerPreview to 5
set thisFolder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "Finder"
activate
open folder thisFolder
select every item in folder thisFolder
delay 2
set fileCount to (count items in (get selection)) # (count files in folder thisFolder) is faster but counts also .DS_Store and other invisible files
if fileCount = 0 then
beep
return
end if
end tell
pressSpaceInFinder()
delay timePerPreview / 2 # first it has to open the window which seems to need a little time
repeat fileCount - 1 times # -1 because the first item is already displayed
delay timePerPreview
# do shell script "sleep" & space & timePerPreview as text
tell application "System Events"
tell application process "Finder"
set frontmost to true
# cursor right = 124, left = 123
key code 124
end tell
end tell
end repeat
delay timePerPreview
pressSpaceInFinder()
on pressSpaceInFinder()
tell application "System Events"
tell application process "Finder"
set frontmost to true
keystroke space
end tell
end tell
end pressSpaceInFinder
Be aware that it is hard to stop the script since the Finder gets activated every second. Will see if I can add a check to see if the Preview window is open and if not, stop the script.
Also: (without that check) the script will, when the Preview window is open before running, close it but you can just press the space key to open it again.
Also, the first part (getting the file count) isn't so great and may fail when the delay is to short. We could scan the whole folder for images and only count / select those (it has a file template for the scanner, see menu File) but of course you can remove the whole counting thing and just do repeat 1000 times.

Detect the end of a Spotify track with Applescript?

I'm writing an Applescript that displays a notification with the name, album, and artist of the current Spotify track. Yes, I'm aware there is already an app that does this, but even with the latest version, it's very buggy and behaves erratically.
So far, I have a good little script. Here it is:
repeat until application "Spotify" is not running
tell application "Spotify"
set theSong to name of current track
set theAlbum to album of current track
set theArtist to artist of current track
set theTime to player position
set theDuration to duration of current track
end tell
(* This sets the amount of delay to the length of the song minus the current place in the song, which leaves us with the amount of time left until the next song plays.*)
set delayTime to theDuration - theTime + 0.5
tell application "Spotify"
if player state is playing then
display notification theArtist with title theSong subtitle theAlbum
end if
delay delayTime
end tell
end repeat
Basically, the problem with this is if I pause or change the track, delay stays the same and thus doesn't display the notification when the song starts.
I used delay in this script because I don't really know a better way of checking for the end of a track. Is there any Applescript event for the end of a song (or the beginning of a new one?)
Thanks.
P.S., If this question is too complicated, feel free to ask more.
Use a loop to check the current track, exit the loop when the song change
repeat until application "Spotify" is not running
tell application "Spotify"
set theSong to name of current track
set theAlbum to album of current track
set theArtist to artist of current track
set thisTrack to current track
if player state is playing then
display notification theArtist with title theSong subtitle theAlbum
repeat until thisTrack is current track
delay 3
end repeat
end if
end tell
end repeat

Preview songs in Spotify with Applescript - NeedleDrop from Doug

We all know Doug's applescripts for iTunes. However, with the growth of streaming music, I don't use iTunes so much anymore, but rather Spotify.
I was trying to adapt the NeedleDrop script to preview songs. The script allows to set
a - A start from given time, f.e. start playing the songs at 30 seconds
b - Play that song for a given time, f.e. 10 seconds.
The problem is the script starts the first song at the given time and plays for the given time, but then, the next songs play for a given time, but start at the beginning instead of the given value in time.
The script is under GNU license by Doug and the license is included.
I'm posting both the script and the exported app from it. If anyone has an idea ?!
LINK: Download
Here is the script provided in the download file:
-- handler to get a number from user
to get_a_number(pmpt, addenda, defnum)
set rez to (display dialog addenda & pmpt default answer defnum buttons {"Cancel ", "OK"} default button 2 with title "Needle Drop")
if button returned of rez starts with "cancel" then tell me to quit
set myNumber to text returned of rez
try
if myNumber is "" then get_a_number(pmpt, "Enter only numbers..." & return & return, defnum)
return (myNumber as integer)
on error -- m number n
get_a_number(pmpt, "Enter only numbers..." & return & return, defnum)
end try
end get_a_number
global start_time -- seconds into each track to begin playing
global needle_drop_interval -- seconds to play each track
on run
-- get number of seconds between songs
set needle_drop_interval to my get_a_number("Play each track for how many seconds?", "", "10")
-- get seconds into each track to play
set start_time to my get_a_number("How many seconds into each track to start playing?", "", "10")
-- play first song in the playlist
tell application "Spotify"
activate
set player position to start_time
play
delay needle_drop_interval
end tell
end run
on idle
tell application "Spotify"
if player state is not playing then tell me to quit
pause
next track
set player position to start_time
play
end tell
return needle_drop_interval
end idle
on quit
try
tell application "Spotify" to stop
end try
continue quit
error number -128
end quit
The provided script works fine for offline playlists but not for streamed ones. Spotify probably doesn't have time to buffer the track or something otherwise.
When I added a delay to give spotify time to buffer it worked fine. (You can experiment with shorter and longer delays)
tell application "Spotify"
if player state is not playing then tell me to quit
next track
pause
set player position to start_time
delay 1
play
end tell
Note the row switch between 'next track' and 'pause'
Amazing !
However, after some songs, the bug still happened sometimes, which seems to get totally fixed by adding a delay before the set player position.
This could sound contradictory, but worked better for me.
Thanks !
on idle
tell application "Spotify"
if player state is not playing then tell me to quit
next track
pause
delay 1
set player position to start_time
play
end tell
return needle_drop_interval
end idle

Resources