Changing the path to a file in iTunes with AppleScript - macos

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

Related

Get path to original item of a file alias when it's missing

I'm trying to write a script to correct the path to an original item of a file alias. I know what's wrong with the path (I moved a folder), so if I can get what the system thinks the path is, I can fix it.
The problem is that the script simply reports that the original can't be found and not where the original should be.
Is there a way to grab that from with AppleScript? It's there in the Get Info... box, so it's stored in the file. I suspect I could use some bash way to get it with a do shell script, but I'm curious about staying within AS.
Years ago, I also had issue with my alias, when changing folders. This is part of the script I made which looks into Get Info window. Since, may be the Get Info window has been changed on last OS version, therefore, some adjustments are required, but it gives you the direction to go:
on Origine_Alias(F_Alias)
set R to ""
tell application "Finder" to open information window of file (F_Alias as text)
tell application "System Events" to tell process "Finder" to set R to value of static text 18 of scroll area 1 of front window
tell application "Finder" to close front window
return R
end Origine_Alias
The returned string is in Unix format: /folder/my/file

Mac Automator - "Move finder items" using variable

I have a weird issue with mac automator (on Mojave).
What I want: Take a pdf > save each page as png to same folder as pdf
What happens: Takes a pdf > all pngs are saved to desktop
Here is the automator after a test run:
You can see on the screenshot how variable "pdfPath" actually is set correctly to Test folder. But then after moving it does remove the files out of system folder, but not to pdfPath but to Desktop. Important: if I manually pick any folder, it will save it there and not to desktop, so still somehow related to the pdfPath variable?
Anybody an idea why it happens and how to fix?
Automator just gets frustrating like this at times. The Move Finder Items action doesn’t appear to work with variables in this situation, even though you can drag and select them - when using a variable it defaults to the Desktop. As a workaround you can replace the Move Finder Items action with a Get Value of Variable for pdfPath to add it to the input items, then add a Run AppleScript action to do the moving:
on run {input, parameters}
if (count input) < 2 then error "No Items to move.“
set destination to last item of input
tell application "Finder" to move (items 1 thru -2 of input) to destination
end run
You might also have to clean out previous results from the temporary folder to keep the rename action happy.
Ok, I figured it out with some help in the meantime. For anybody coming across and looking for solution, the path I saved in the Applescript part was showing up nicely in the result, but in the end, the path was in the wrong format. Following code works like a charm:
on run {input, parameters}
tell application "Finder" to return POSIX path of (container of (item 1 of input) as alias) as text
end run

Search and delete a particular track in iTunes using 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)

Applescripting a Reminder as an add- folder action

I'm new to applescript but I want to set up a folder action that:
1. Recognises when a file is added to a folder
2. Tags said folder red
3. Adds a Reminder to the "Downloads" reminder list that has the name of the newly-added file as the body text of the reminder
Using google and Applescript's record function I've frankenscripted this together so far
property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
set FILEname to name of (added_items)
set label index of folder "untitled folder" of folder "Desktop" of folder "heyjeremyoates" of folder "Users" of startup disk to 2
end tell
tell application "Reminders"
set mylist to list "Downloads"
tell mylist
make new reminder with properties {name:"D/L Complete", body:FILEname, due date:(current date)}
end tell
end tell
end try
end adding folder items to
Darn thing won't work. Infuriating. I tested it as a folder action with "test" as the name and body of the reminder and it worked fine. I'm pretty sure I've gone wrong somewhere in setting FILEname as the name of the newly copied item because the script as it is now no longer turns the folder red.
The idea behind this is so I can see, from my iPhone/iPad, how many large/scheduled downloads to my home mac (both torrents and large work files - I'll have a seperate folder action and reminder list for each download folder) there are that are yet to be managed.
It seemed like setting up a Growl/Prowl combo was wasteful if iCloud/Reminders and a dozen lines of code could deliver what I wanted anyway. Ideally I'll write a second applescript that will delete the reminder when I rename or move the related file, and though I haven't even thought about how that would work if anyone has any suggestions for it I'd be super grateful
It is a pity you can't (natively) have OSX notifications pushed to an iOS device linked to the same iCloud account though (with the appropriate granularity)
But I digress - can anyone see what I'm screwing up here?
Thanks in advance for even reading this far
added_items is a list of aliases, and name of (added_items) resulted in an error.
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set label index of this_folder to 2
repeat with f in added_items
set n to name of f
tell application "Reminders" to tell list "Downloads"
make new reminder with properties {name:"D/L Complete", body:n, due date:(current date)}
end tell
end repeat
end tell
end adding folder items to
(Save the script in ~/Library/Workflows/Applications/Folder Actions/ and enable the folder action from Folder Actions Setup.)

AppleScript "Save As" HTML in TextEdit

I want AppleScript to loop through a set of of RTF files in folder and save them as HTML files.
This is my simple code so far. The XXXX is where I'm struggling:
tell application "Finder"
set source_folder to choose folder
set aList to every file in source_folder
repeat with i from 1 to number of items in aList
tell application "TextEdit"
set aFile to (item i of aList)
save as aFile XXXXXXXXX
end tell
end repeat
end tell
I'm really new to this... any help much appreciated.
You don't need TextEdit for this. There is a command line program textutil which will do the job without all the opening and saving stuff required with TextEdit. We can fix your TextEdit script (it has a few errors) but try this first and let us know if it does the job for you. The html files will have the same name but with the html extension and will be located in source_folder. The ouput path can be changed in the code by using the "-output" switch of textutil. See "man textutil" if you want to look at everything it can do.
And a general question... what is a RTD file? Do you mean rtf or rtfd? Textutil will work with rtf/rtfd but not rtd, so I hope that isn't really your file type.
set source_folder to choose folder with prompt "Choose a source folder."
set output_folder to choose folder with prompt "Choose an output folder."
tell application "Finder"
set theFiles to (files of entire contents of source_folder) as alias list
end tell
repeat with aFile in theFiles
tell application "Finder"
set fileName to name of aFile
set fileExt to name extension of aFile
end tell
set outputPath to (output_folder as text) & text 1 thru -((count of fileExt) + 1) of fileName & "html"
do shell script "/usr/bin/textutil -convert html -output " & quoted form of POSIX path of outputPath & space & quoted form of POSIX path of aFile
end repeat
You mention you are new to applescript, so I'll give you some general pointers you should keep in mind when writing applescript code.
Avoid putting tell blocks of code inside each other. You have tell app TextEdit inside tell app Finder. That's bad. Doing this is a source of many conflicts because you are basically telling the Finder to tell TextEdit to do something. That's not good because commands can get confused and it's really hard to debug these kinds of issues. So keep your tell blocks separate.
Avoid telling an application to perform a command that is not in its applescript dictionary. You should only tell an application to do commands that it knows and an application only knows about the commands in its dictionary. So for example, you are telling the Finder to "choose folder". The Finder does not know that command. That's an applescript command. So doing as you have done is another possible source of errors. In this case that's a simple command and it will work but in general avoid doing this.
Regarding the Finder, you should avoid using it too much. The Finder is a major program on your computer and is often busy doing computer related stuff. As such it's best to only use it when necessary. As an example you can see in my code that I removed the "choose folder" and the repeat loop from the Finder. I purposely appended "as alias list" to the end of the Finder command to make the list of files usable outside of the Finder tell block of code. Of course use the Finder if needed but it's best to not use it if you don't need it.
Use the applescript dictionary of your applications. As mentioned above, the dictionary lists all of the terms and the syntax that an application understands (granted the dictionaries are difficult to understand but you will get better at it the more you use them). Under the file menu of AppleScript Editor choose "Open dictionary" and a list of all the applications that understand applescript is shown. Choose an application from that to see its dictionary. So for example, you are trying to figure out TextEdit's "save as" command. You can usually get good direction from the dictionary so you should take a look at that. Use the search field to search!
So I hope that helps! Good luck.

Resources