I wrote an Applescript that would monitor a folder for new input. On receiving the new item, it would open iTunes, create a playlist and add the new files into the playlist. However, when I attach the script as a Folder Action via Folder Action Setup and add a new item, nothing happens. I have placed the script inside /Library/Scripts/Folder Action Scripts as well as ~/Library/Scripts/Folder Action Scripts but it still doesn't fire. Here is my code below:
global album_name
global artist_album
on adding folder items to this_folder after receiving added_items
--get the name of the added folder
repeat with x in added_items
copy name of x as string to playlist_name
set AppleScript's text item delimeters to "-"
set delimited_playlist_name to every text item of playlist_name
set artist_name to text item 1 of delimited_playlist_name
set album_name to text item 2 of delimited_playlist_name
set AppleScript's text item delimeters to ""
end repeat
tell "Finder" to activate "iTunes"
tell application "iTunes"
if not (exists playlist album_name) then
--make iTunes playlist
make playlist with properties {name:album_name}
--add the tracks to the iTunes playlist
repeat with song in playlist_name
add song to playlist album_name
set album of song to album_name
set artist of song to artist_name
set comment of song to ""
set composer of song to ""
set grouping of song to ""
set description of song to ""
set long description of song to ""
set (volume adjustment) of song to 100
end repeat
set view of browser window to album_name
end if
end tell
end adding folder items to
I'm running Mac OS 10.8.4
In the future
Test your code in the Applescript Editor, it helps a lot :)
If you dont use Applescript Editor, to get useful messages, use display dialog entries throughout your code to see how far it goes and what is going on.
So, assuming that the problem is with your code, I rewrote it as an simple Applescript and got it to work on my Snow Leopard (10.6.8) system.
It also runs ok from inside Automator as a Folder Action with the code in a Run Applescript Action item. Once saved from inside Automator it did activate when new files were added to the folder and run ok.
PS: Since you didn't provide any details, I had to guesstimate from your code what you were trying to do. It is far from optimal but it will get you going...
HTH
# remove this line for Applscript Action in Automator...
set input to (choose file with multiple selections allowed)
set playlist_name to {}
#display dialog "Delimeter set to -"
set AppleScript's text item delimiters to "-"
repeat with x in input
#display dialog "Item: " & (x as text)
tell application "Finder"
set fl to name of x
display dialog "Filename: " & fl
end tell
# bring back focus to the editor ## TESTING ONLY
activate me
# append item to the list
copy (fl as text) to the end of playlist_name
display dialog "Playlist count: " & length of playlist_name
set artist_name to text item 1 of fl
display dialog "Artist: " & artist_name
set album_name to text item 2 of fl
display dialog "Album: " & album_name
end repeat
#display dialog "Activating iTunes..."
#activate "iTunes"
#display dialog "iTunes activated!"
tell application "iTunes"
if not (exists playlist album_name) then
display dialog "Making new playlist: " & album_name
make playlist with properties {name:album_name}
display dialog "Changing view..."
set view of browser window 1 to playlist album_name
# add the tracks to the iTunes playlist
repeat with song in input
display dialog "Adding: " & song
set newtrack to (add song to playlist album_name)
set album of newtrack to album_name
set artist of newtrack to artist_name
set comment of newtrack to ""
set composer of newtrack to ""
set grouping of newtrack to ""
set description of newtrack to ""
set long description of newtrack to ""
set (volume adjustment) of newtrack to 100
end repeat
else
display dialog "iTunes playlist already exists!"
end if
end tell
Related
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.
I have been making a simple script for my sister, but it does not work. It calls for an integer? I did not specify it in my program, but it calls it anyway...?
My Script:
activate
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons
{"Cancel", "Start"} default button "Start"
property documentFolder : "documents"
tell application "Finder" to (get name of every disk whose ejectable is true)
try
set kindleLocation to ¬
((choose from list result with prompt "Select your Kindle from the list:") as text)
end try
try
set bookFiles to ¬
((choose file with prompt ¬
"Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc",
"public.data.mobi", "public.plain-text", "com.adobe.pdf"} with
multiple selections allowed) as text)
end try
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
tell application "Finder"
if not (exists folder documentFolder of kindleLocation) then
make new folder at kindleLocation with properties {name:documentFolder}
end if
end tell
set fullKindlePath to POSIX path of (kindleLocation as alias) & "documents"
tell application "Finder"
move (bookFiles) to fullKindlePath
end tell
display dialog "Process has been done! Please eject your kindle and the files will be on the home screen of your Kindle." with title
"Kindle Book Uploader by Jeremy Zhang"
And the result from running it:
tell current application
activate
end tell
tell application "AppleScript Editor"
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons
{"Cancel", "Start"} default button "Start"
-- {button returned:"Start"}
end tell
tell application "Finder"
get name of every disk whose ejectable = true
-- {"JEREMY DISK"}
end tell
tell application "AppleScript Editor"
choose from list {"JEREMY DISK"} with prompt "Select your Kindle from the list:"
-- {"JEREMY DISK"}
choose file with prompt "Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc",
"public.data.mobi", "public.plain-text", "com.adobe.pdf"} with
multiple selections allowed
-- {alias "Macintosh HD:Users:JeremyZhang:Downloads:5 ETS SAT S.pdf"}
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
-- {button returned:"OK"}
Result:
error "Canât make \"documents\" into type integer." number -1700 from "documents" to integer
What am I doing wrong? Can you please correct me?
AppleScript Editor V: 2.5 (138)
AppleScript 2.2.3
It's a somewhat misleading error. It's telling you that documentFolder is the wrong type. But there are a couple other issues with the script, too. This version should work -- see comments, but try running this as is (I removed the option-return continuation character you were using; I suspect it doesn't translate well on this forum) :
activate
display dialog "Click Start to start importing your own kindle books!" with title "Kindle Book Uploader by Jeremy Zhang" buttons {"Cancel", "Start"} default button "Start"
property documentFolder : "documents"
tell application "Finder" to (get name of every disk whose ejectable is true)
try
set kindleLocation to ((choose from list result with prompt "Select your Kindle from the list:") as text)
--display dialog class of kindleLocation -- class is text
end try
try
set bookFiles to ((choose file with prompt "Select kindle files to import:" of type {"public.html", "public.rtf", "com.microsoft.word.doc", "public.data.mobi", "public.plain-text", "com.adobe.pdf"} with multiple selections allowed) as text)
end try
display dialog "Please wait while the application copies the kindle books..." with title "Kindle Book Uploader by Jeremy Zhang"
tell application "Finder"
--coerce kindleLocation to alias
if not (exists folder documentFolder of alias kindleLocation) then
make new folder at kindleLocation with properties {name:documentFolder}
end if
end tell
--don't use posix ... but you already have variables to build this
--set fullKindlePath to (kindleLocation) & ":documents:"--could've done this, but you don't need this, you can use same construction as above
tell application "Finder"
move (bookFiles) to folder documentFolder of alias kindleLocation
end tell
display dialog "Process has been done! Please eject your kindle and the files will be on the home screen of your Kindle." with title "Kindle Book Uploader by Jeremy Zhang"
i'm newbies with AppleScript and i wanted to create an UI Selector for many of AppleScripts.
Example, i've testA.scpt, testB.scpt, testC.scpt and i want to run just one of these scripts by choosing from a list of choices in a window (UI).
it's possible ?
so, it's possible, could you help me and give an example !?
i think is a very similar code but i'm not sure :
set theName to (choose from list {"testA", "testB", "testC"})
if theName is false then
display dialog "You clicked cancel to exit." with icon stop buttons {"Exit"} default button {"Exit"}
else
set theName to (item 1 of theName)
display dialog theName with icon note buttons {"Info"} default button {"Info"}
end if
Give this a try. It's based on your scripts being in the same directory as this script.
set theName to (choose from list {"testA", "testB", "testC"})
if theName is false then
display dialog "You clicked cancel to exit." with icon stop buttons {"Exit"} default button {"Exit"}
else
set theName to (item 1 of theName)
tell application "Finder"
set the_script_path to ((container of the (path to me)) as text) & theName & ".scpt"
set the_script to load script alias the_script_path
end tell
run script the_script
end if
I'm trying to make an AppleScript that tells iTunes to play a certain track. Here's my code. I'm baffled, because when I set "theCommand" to "play Shake Up Christmas by artist Train" the script works when I right-click a test e-mail and click "Apply Rules." However, it doesn't work when I tell it to play what the e-mail tells it to play.
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
tell application "Mail"
repeat with thisMessage in messageList
try
set theCommand to content of thisMessage as string
on error errMsg
display alert errMsg
end try
end repeat
end tell
tell application "iTunes"
if theCommand contains "play" then
if theCommand is equal to "play" then
play
else
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to "play "
set subject to text items of theCommand
set text item delimiters of AppleScript to ""
set subject to "" & subject
set AppleScript's text item delimiters to " by artist "
set delimitedList to every text item of subject
set theTrack to the first item of delimitedList
try
set theArtist to the second item of delimitedList
set artistIsDefined to true
on error
set artistIsDefined to false
end try
set AppleScript's text item delimiters to prevTIDs
if artistIsDefined is true then
try
play (every track in playlist 1 whose artist is theArtist and name is theTrack)
say "Playing " & theTrack
on error errMsg
say errMsg
end try
else
play (every track in playlist 1 whose name is theTrack)
end if
end if
else if theCommand is equal to "pause" then
pause {}
else if theCommand is equal to "stop" then
stop {}
end if
end tell
end perform mail action with messages
end using terms from
The Mac will respond to an e-mail by saying "Playing (track)," but it won't play the track. The fact that the file is on a Time Capsule shouldn't matter, as iTunes can access it. (iTunes would have shown an exclamation mark to the left of the track name if it couldn't.)
My guess is that you need to change the word "every" to "first" in this command...
play (every track in playlist 1 whose artist is theArtist and name is theTrack)
I didn't test my theory, but I think by using the word "every" you get a list of tracks from that command (even if only 1 track is in the list). And iTunes doesn't know how to play a list. iTunes can play a track. So by using "first" you will get the first track found and thus it should work. Alternatively you could get the first item of the list... play (item 1 of (every track...)).
The script is as below:
tell application "Finder"
set destinFolder to (choose folder)
try
move selection to destinFolder
on error vMessage number -15267
display dialog "" & vMessage
end try
end tell
Basically,if a file with the same name has already existed in the destination folder, I want users have the choice to duplicate,replace or just skip that file and continue the next file movement using AppleScript. Just like the pic below.
Update: I know how to display a dialog like the one above,the problem is I don't know how to implement the "duplicate,replace or just skip" logic in AppleScript. And besides, I want to keep this line:
move selection to destinFolder
Because this line of code show a proper moving progress percentage in a single dialog, using repeat will lose that benefit.
You can try something like this:
set destinFolder to (choose folder)
set destItems to every paragraph of (do shell script "ls " & quoted form of (POSIX path of destinFolder))
tell application "Finder"
set mySelection to selection
repeat with anItem in mySelection
set itemName to anItem's name
if itemName is in destItems then
display alert "An item named " & itemName & " already exists in this location. Do you want to replace it with the one you're moving?" buttons {"Skip", "Replace"} default button "Replace"
if button returned of the result = "Replace" then move anItem to destinFolder with replacing
else
try
move anItem to destinFolder
end try
end if
end repeat
end tell
or this:
set destinFolder to (choose folder)
tell application "Finder"
set mySelection to selection
repeat with anItem in mySelection
try
move anItem to destinFolder
on error errMsg number errNum
if errNum = -15267 then
display alert "An item named " & itemName & " already exists in this location. Do you want to replace it with the one you're moving?" buttons {"Skip", "Replace"} default button "Replace"
if button returned of the result = "Replace" then move anItem to destinFolder with replacing
else
tell me
activate
display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
end tell
end if
end try
end repeat
end tell
EDIT
This script will not give you a choice for each item that exists in the destination folder
set destinFolder to (choose folder)
tell application "Finder"
set mySelection to selection
try
move mySelection to destinFolder
on error errMsg number errNum
if errNum = -15267 then
display alert "One or more items already exist in this location. Do you want to replace them with the ones you're moving?" buttons {"Skip", "Replace"} default button "Replace"
if button returned of the result = "Replace" then move mySelection to destinFolder with replacing
else
tell me
activate
display alert errMsg & return & return & "Error number" & errNum buttons "Cancel"
end tell
end if
end try
end tell
Its not posible to display finder copy UI using applescript. You should implement your own UI.
your script will display this.
Take a look at Mac Finder native copy dialog article.