Filling in gaps in itunes - applescript

I am new to appplescript and programming in general. would someone be so kind as to look over my code. I know its not in proper applescript syntax yet, as I struggled to find that information.
tell application iTunes
for each track in library playlist{ #all listed tracks, regardless of what files i have. may include dead links
set tr to track name
if file location is missing then search for it at external/Music/iTunes else messagebox(tr no file)
if search has result cut and paste to Music/itunes
check if file now exists else messagebox(tr error)
} end tell

I'll get you started. Here's how you can find the song name and artist of all tracks not found on your computer. You'll have to build on this to do the rest of your stuff.
set missingTracks to {}
tell application "iTunes"
set alltracks to tracks of library playlist 1
repeat with aTrack in alltracks
set theLocation to location of aTrack
set doesExist to my fileExists(theLocation)
if not doesExist then
set thisInfo to {songName:name of aTrack, songArtist:artist of aTrack}
set end of missingTracks to thisInfo
end if
end repeat
end tell
return missingTracks
on fileExists(theLocation)
tell application "Finder"
if exists theLocation then
return true
else
return false
end if
end tell
end fileExists

Related

How can I delete Apple Music tracks from my Library using AppleScript?

My Apple Music library is too big. I want to weed it out by removing a whole load of tracks that I have never listened to. I already did the same thing successfully with playlists but my script isn't working to remove tracks:
tell application "Music"
activate
set mytracks_list to (get the id of (every track whose loved is false and played count is 0 and rating is less than 60))
repeat with mytrack_id in mytracks_list
delete (the track whose id is mytrack_id)
end repeat
end tell
The mytracks_list is populated with no problems. The error message I get is:
error "Can’t get track whose id = item 1 of {130098, [............] }
Am I doing something wrong, and can it be made to work?
P.S. This is what worked for my playlists:
tell application "Music"
activate
set myplaylists_to_delete to (get the name of every playlist whose name does not contain "Adrian" and name does not contain "Loved" and name does not contain "Shazam" and name does not contain "Library" and name is not "Music" and name does not contain "Recent" and name does not contain "5 Stars" and name does not contain "Duo")
repeat with myplaylist in myplaylists_to_delete
delete playlist myplaylist
end repeat
end tell
Did you try:
tell app "Music"
delete every track whose loved is false and played count is 0 and rating is less than 60
end tell
Well-designed, well-implemented “AppleScriptable" apps can usually apply a command to multiple objects; you don’t need to iterate the objects yourself. (Hint: Apple event IPC = RPC + queries, not OOP.)

Getting last encoded file information from Apple Compressor

So I'm a novice with AppleScript but learning a lot. I'm trying to create an AppleScript to let me know when Apple Compressor is finished encoding and notify me if it was successful or not. I have the part down about telling when compressor is done and the notification but having difficulty getting the file name and status (complete/fail/cancelled) from Compressor's history log.
Originally tried with UI scripting, but Compressor's window hierarchy list changes with every new encode. So now onto pulling it from the history log, which is in the user folder "Library/Application Support/" folder like this:
And then, the file itself contains the status (whether it was successful or failed) near the bottom of the file like this:
So getting confused about how to A) find the last file and its name, and B) get the value of the last "ElementStatusState" in the document.
This is where I started but get errors when trying to resolve the path to the folder:
tell application "Finder"
set latestFile to (last item of (sort (get files in folder "~/Library/Application Support/Compressor/History/V4" of application "System Events") by name)) as text
set fileName to latestFile's name
end tell
This code throws an error about not being able to make it into the expected file type but also, I can see that It's grabbing a file that's not the last one modified.
In the end I want 2 variables that are 1) theFileName = ie name of encoded movie(s), and 2) theStatus = ie number 4,5, or 6)
Any ideas out there?
You'll want to do something like the script below. This script finds the most recently modified file, then drills down through the plist file to find the various status numbers, and the names of the records they are logged in. I'm assuming that every file Compressor generates has the same structure; if not, you may find you have to alter the script. But this should give you an idea how to do that.
Names and status values are stored in the nameList and valueList variables, in order of depth.
set filePath to POSIX file "~/Library/Application Support/Compressor/History/V4"
-- sort the file list by modification date, then get the last item.
tell application "Finder"
set mostRecentFile to last item of (sort files of folder filePath by modification date) as alias
end tell
set mostRecentFilePath to POSIX path of mostRecentFile
set nameList to {}
set valueList to {}
tell application "System Events"
set plistFile to property list file mostRecentFilePath
tell plistFile
tell (first property list item whose kind is record)
copy value of property list item "ElementInfoName" to end of nameList
copy value of property list item "ElementStatusState" to end of valueList
tell (first property list item whose kind is list)
tell (first property list item whose kind is record)
copy value of property list item "ElementInfoName" to end of nameList
copy value of property list item "ElementStatusState" to end of valueList
tell (first property list item whose kind is list)
tell (first property list item whose kind is record)
copy value of property list item "ElementInfoName" to end of nameList
copy value of property list item "ElementStatusState" to end of valueList
end tell
end tell
end tell
end tell
end tell
end tell
end tell

Check if two files are the same in Applescript

I'm trying to get the bibliographic information of the currently open PDF in Skim.
To do that, I am comparing the file to the linked files of all the publications I have in BibDesk.
But for some reason, that comparison doesn't work:
tell application "Skim"
set theFile to the file of the front document
tell application "BibDesk"
repeat with currentPub in publications of front document
set bibFile to linked file of currentPub
if bibFile = theFile then
say "lala"
end if
end repeat
end tell
end tell
The script does get both theFile and bibFile correctly, but comparing them doesn't work. Why is that and what do I need to do differently?
Turns out it's super easy: you just have to compare with as string:
tell application "Skim"
set theFile to the file of the front document
tell application "BibDesk"
repeat with currentPub in publications of front document
set bibFile to linked file of currentPub
if bibFile as string = theFile as string then
say "lala"
end if
end repeat
end tell
end tell

iTunes AppleScript: query all playlists that contain a certain track

Working on a complex AppleScript for iTunes. One task is to accumulate a list of all playlists which contain a given track. I have this track object from somewhere else (a selection or whatever).
Currently, I've got a snippet something like this:
on containingPlaylists(theTrack)
tell application "iTunes"
set librarySource to the source named "Library"
set candidateLists to every user playlist in librarySource
set candidateId to (get id of theTrack)
set matchLists to {}
repeat with candidateList in candidateLists
set matchTracks to (file tracks in candidateList whose id = candidateId)
if (count of matchTracks) > 0 then
copy candidateList to end of matchLists
end if
end repeat
return matchLists
end tell
end containingPlaylists
This works but requires one Apple Event per playlist in the loop, which is expensive (perf) and throws away the intermediate results. What I'd RATHER do is something all in one query:
set matchLists to every playlist in librarySource whose file tracks contain theTrack
But this of course doesn't work (the particular error is "Handler only handles single objects." but not sure if that's insightful). I'm really just not sure if the language/app supports a query like this.
Can anyone confirm/deny/offer any insight? Thanks!
You can use this (work on iTunes 11 and 12):
tell application "iTunes"
set theTrack to item 1 of (get selection)
return user playlists of theTrack
end tell
Updated --
In the AppleScript dictionary:
artwork n [inh. item] : a piece of art within a track |
elements : contained by tracks. So artworks of thisTrack works
track n [inh. item] : playable audio source |
elements : contains artworks; contained by playlists. So playlists of thisTrack works, you can use user playlists of thisTrack
In iTunes.h (ObjC scripting bridge):
#interface iTunesTrack : iTunesItem
- (SBElementArray *) artworks;
it's not possible because playlists is not in the SBElementArray's list.
But I do not know why there is a difference between the AppleScript dictionary and the iTunes.h file.
I too wish a whose clause like that could be used. But alas. Someone else might come up with a better plan, but I'm pretty sure this is how I would find the playlists containing the selected track (it may be the most efficient):
set persisID to persistent ID of selection
set pp to playlists
set playListsWithIt to {}
repeat with p in pp
set tt to (tracks of p whose persistent ID is persisID)
if tt ≠ {} then set playListsWithIt to (playListsWithIt & (id of p))
end repeat
Then I can use those IDs for the next step. This includes, of course, playlists like "Recently Added", which may or may not be what you want; you'd have to put another step in there to 'filter' out such a result.

Check for part of a folder name in applescript

I have an applescript that checks for one of several external HDs I connect to my computer and makes an index of their contents. All the HDs follow the naming scheme HK_12345 where the first two letters are always the same but the numbers are different for each drive. I want the script to only check for drives with the 'HK' designation regardless of the ending numbers. I have this but it is not working. Any advice?
set folderContains to "HK_"
tell application "Finder"
set triggerFolder to folder whose name contains folderContains
end tell
tell application "Finder"
if folder triggerFolder exists then
-- some other code
end if
end tell
You should check for disks not folders:
set diskContains to "HK_"
tell application "Finder"
set selectedDisks to disks whose name contains diskContains
-- some other code
end tell

Resources