Search and delete a particular track in iTunes using Applescript - applescript

I am using iTunes to convert a large .aif file to a much smaller .mp3 file. The filename changes each day because I am adding the date to the name. So, a file named "abcxyz 2-2-2014" gets converted in iTunes. After the conversion, I want to delete it from iTunes. I'm trying to use Applescript to search for the file and delete it. I'm trying this:
on deleteTrack(trackName)
tell application "iTunes"
set theTrack to track named trackName of playlist "Library"
set songFile to location of theTrack
delete theTrack
end tell
tell application "Finder" to delete songFile
end deleteTrack
on run
tell application "iTunes"
set result to (file tracks whose name contains "abcxyz")
repeat with t in result
deleteTrack(name of t as string)
end repeat
end tell
end run
I found the deleteTrack routine which works perfectly if you pass it a string like this:
on run
deleteTrack("abcxyz 2-2-2014")
end run
But that requires that I know the exact name of the track, which I don't. The error that I get is "Can't continue deleteTrack" ..with the deleteTrack(name of t as string) line selected in Applescript Editor.
Thanks for any help.

You should re-read the material about AppleScript's handlers. Because your handler call is inside of a tell application "iTunes" block, AppleScript is looking in iTunes for deleteTrack. Try this instead:
my deleteTrack(name of t as string)

Related

Apple Script: how do I watch for new download file with specific name to trigger a script?

I have a script that downloads a report from an online service, waits a specified amount of time e.g. 30secs for the file to actually download and then renames the file. The script then repeats
What I would like to do is instead of putting a static delay in the script, to create a trigger that looks for the new file and once it appears triggers the renaming portion of the script.
The downloaded file name is constant.
The current portion of the script looks like this
`
tell application "Finder" to activate
tell application "System Events"
delay Wait_Time
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
ultimately what I would like is to replace the delay Wait_Time with a something like:
repeat until file name found
Search Folder for "file name"
end repeat
select new file
rename new file to clipboard
obviously this wont work but it kind of captures my logic.
I have also tried a few other possible solutions but Im new to AppleScript and not overly confident that these approaches are suitable for my application.
The below is an example of a solution I have tried. This is just an example and im not sure if this particular code can even get me to where I need to be. I thought I would include it to show just how lost I am.
set excludes to {"Folder", "Application"}
tell application "Finder"
set search_folder to folder "Macintosh HD:Users:XXXXXXXX:Library:CloudStorage:OneDrive-Flinders:Flinders Connect Stats:Source Stats:Cisco Finesse:Agent State Summary by Interval Report"
set foundItems to (every item in search_folder whose name contains "Agent State Summary by Interval Report" and kind is not in excludes) as alias list
if foundItems is {} then return
repeat with once from 1 to 100
try
if (count of foundItems) = 1 then
tell application "Finder" to activate
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
exit repeat
end if
end try
end repeat
return
end tell
Im sure you can see I have no idea what im doing.
Anyway any help would be awesome!
I have found a solution... if anyone has ideas for improvement please let me know.
tell application "Finder" to activate
tell application "System Events"
repeat until (exists (files of folder folderPath whose name contains "Agent State Summary by Interval Report"))
delay 1
end repeat
end tell
tell application "System Events"
key code 125
tell application "Finder"
set name of (selection as alias) to (the clipboard)
end tell
end tell
end repeat
say "Finished!"
display dialog "Completed"
The original solution was found here AppleScript: How to repeat a search for a file until it is found?

Hide duplicate files in macOS search results?

I don't want to delete duplicate files, but I only want to see one instance of each.
Prioritization among duplicates doesn't matter in this case.
///
The actual scenario:
I want to create a VLC playlist containing every video I have ever downloaded from a particular domain. The files are not well-organized and many exist in more than one location on my computer. Consequently, a Finder search for "Where from" --> [the domain] returns many duplicate files. Which means I can't just drag and drop the search results into VLC without there being many duplicates.
I don't want to deduplicate the files themselves.
How can I make this happen?
It is not possible to hide duplicates in the Finder search because there is no option for filtering duplicates in the Finder search.
But your job is to search, then create a playlist. This can be done by dragging and dropping your selection onto the next droplet, which you can call "VLC Playlist Creator". (Or turn it into a "Make VLC Playlist" service using Automator). It will filter for you the duplicates and open Save Playlist Dialog of VLC.app.
on open theAliases
set {namesList, moviesList} to {{}, {}}
repeat with i from 1 to count theAliases
set anAlias to item i of theAliases
tell application "Finder" to set theName to name of anAlias
if not (namesList contains theName) then
set end of namesList to theName
set end of moviesList to anAlias
end if
end repeat
-- try to restart "VLC" to clear old playlist
if running of application "VLC" then quit application "VLC"
repeat while running of application "VLC"
delay 0.1
end repeat
-- add movies to playlist
tell application "VLC"
open moviesList
stop
activate
end tell
-- save playlist dialog
tell application "System Events" to keystroke "s" using command down
tell application "VLC" to activate
end open

Open/search Spotify track in Apple Music (with Applescript)

I'd like an easy way to switch from a Spotify release to the same release in Apple Music.
I already found a way to search for the currently playing Spotify track in the Apple Music web player with Applescript:
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
open location "https://music.apple.com/search?term=" & currentArtist & " " & currentTrack
end if
end tell
I'd love to:
Open the search in the native Music.app, not the web player. Is this supported?
Ideally not do a search, but go straight to the same release. Maybe with ISRC codes?
Take any selected Spotify track, not just the currently playing one. Looking at the Spotify Applescript dictionary tells me this in not possible.
had a similar problem right now and quickly hacked it out. In my case I want to simply trigger a search on the Music app.
Opened Automator and created a new "Service".
Workflow receives current > "Text" > in "every application".
Here's the AppleScript:
on run {input, parameters}
tell application "Music"
activate
end tell
tell application "System Events"
tell process "Music"
set window_name to name of front window
set value of text field 1 of UI element 1 of row 1 of outline 1 of splitter group 1 of window window_name to input
keystroke ""
key code 36
end tell
end tell
return input
end run
I saved it as "Find on Music" in Automator and now I can select text, right click > Service > Find on Music and Music plops open and shows me the results for the selected text. I hope you can use some parts of it.
I just figured out how to pass text from wherever to the search field in Music, with help from daemon's answer, which no longer works. This should work for what you want to do in conjunction with what you have.
Replace your "open location" line with a variable name for your concatenated string. Add this code below yours and pass that variable in place of 'input' (in my case 'input' is text from any application, which I use to select text of an artist name in an email/webpage/message that I want to send to Music's search).
First it checks to see if the main Music window is open vs the MiniPlayer, and open it if not to enable search via cmd-O, the cmd-F to find, then passes the input and hits return:
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke input
key code 36
end tell
end tell
So, something like this (I don't have Spotify to check that section, but this should work assuming your code there is correct):
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
set spotTrack to currentArtist & " " & currentTrack
end if
end tell
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke spotTrack
key code 36
end tell
end tell
The only thing I couldn't figure out is how to check if the search field is already in focus, because if it is, the cmd-F causes a system alert sound. Generally not an issue as typically you'll search and interact with something else before running this script again, so calling it good. :)

Getting the Filename of a pdf print dialog in osx automator

I'm creating an automator pdf print plugin.
When you choose the print plugin the filename to the pdf is the input (normally /var/something /documentName.pdf)
I would like to get the documentName to use it later in an Rename Finder Item.
I'm using atm applescript to accomplish this.
on run {input, parameters}
tell application "Finder"
set fileName to name of ((POSIX file input) as alias)
end tell
return fileName as string
end run
The problem is that this only works when I put an Ask for Text Action before the applescript which displays the posix path.
If I remove the Ask for Text action the applescript fails.
The workflow is at https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip
I guess it is something simple but this is the first applescript / automator workflow I'm creating.
As I fail on commenting
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken post below.
Thanks!
I created a test workflow with this AppleScript:
on run {input, parameters}
tell app "System Events"
display dialog ((class of input) as string)
end
return input
end run
That displayed "list". I then modified it to:
on run {input, parameters}
tell application "System Events"
display dialog ((class of first item of input) as string)
end tell
return input
end run
That displayed "alias".
So, the input to a PDF workflow is a list of aliases. Write your script with that in mind and it should work. For example, this works:
on run {input, parameters}
tell application "System Events"
display dialog ((name of first item of input) as string)
end tell
return input
end run
When working with AppleScript, it can really help to forget everything you know about file paths. If you think in paths, you will be doing path-math in your head and all of it is unnecessary work. What you want to work with is objects. When doing file operations, you work with alias objects.
If you look at the PDF you are working with in Finder, and go File ▶ Make Alias then you’ll create an alias file. You can drag that alias file all around the file system of the disk it is on, put it in any folder, and when you open the alias file, it will still always open your original PDF, even if you forget what path name your original PDF file has, and even more importantly: the alias will open the PDF even if the PDF has moved to somewhere else in the file system. An alias does all that work for you. You don’t need to know the path names.
In AppleScript, rather than working with files, you work with aliases, and whatever you do to an alias is also done to the original file. So you don’t need to know the path name of a file to change its name — you only have to have an alias of it to work on. You store that alias in a variable.
So what you want to do is set the input PDF alias to a variable, and then later, that variable is what you give Finder to rename. You don’t have to know any paths. It doesn’t matter where the input PDF is stored on the file system — the alias will take care of that.
Here is an example AppleScript that demonstrates the principle of taking an alias as input, and then later renaming that alias (and thus, the original file:)
tell application "Finder"
set theInputFile to (choose file)
-- do a workflow here
set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
end tell
Here is a line-by-line description of the above script:
the opening tell block that specifies we are talking to Finder
show the user a choose file dialog box, and set the alias that is returned by that dialog box to a variable called “theInputFile”
a comment that is a placeholder for whatever workflow steps you might want to do
rename theInputFile to “Renamed” and its original file extension
quit talking to Finder
And even where you want to work with the folder that contains your original input file, or want to know what disk the input file is on, you still don’t need to work with path names:
tell application "Finder"
set theInputFile to (choose file)
set theContainingFolder to open the container of theInputFile
set theInputFileDisk to the disk of theInputFile
end tell
And if you want to know what kind of file the input file is, you don’t have to look at the filename extension and figure it out, you can just say:
set theInputFileKind to the kind of theInputFile
if theInputFileKind is equal to "Portable Document Format (PDF)" then
-- do stuff
end if
And if you want to work in specific folders, such as the home folder, there are special properties for that, like “the path to the home folder” so that the following script opens “~/Public/Drop Box” on any system, no matter what the user name:
tell application "Finder"
activate
set theHomeFolder to the path to the home folder as alias
set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
open theDropBoxFolder
end tell
You can walk around disks and folder structures as objects as shown above, so again, there is no need to think in paths. Think in terms of setting variables to objects that you want to interact with.
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken's post
Thanks!

Changing the path to a file in iTunes with AppleScript

I am trying to write an iTunes script that takes the selected tracks, moves the files to a different folder on my hard drive, and then update their location in iTunes.
The overall flow will be something like this:
Get selection
Determine path to selection
Move items to destination
Update referenced path in iTunes
I used the suggestion from this question to get the path of a selection in iTunes, and I will be able to figure out how to move the files to where I want them, however, now I want to tell iTunes that the file path is actually some place else.
Anybody know how to go about this?
I figured it out. I had a different error that was making me think this is harder then it is. Here's how I got it to work:
tell application "iTunes"
set s to selection
repeat with c in s
if video kind of c is TV show then
set location of c to <destination directory>
<code to move file>
end if
end tell
The basic idea is to set the location property of each file track item to its new file path. For example:
tell application "iTunes"
tell its first browser window
set currentTrack to first item of (get its selection)
set location of currentTrack to POSIX file "/Users/nad/Music/movedfile.m4a"
end tell
end tell

Resources