I'm trying to determine the path of track selected in iTunes using AppleScript. It doesn't seem to be a property of the track class. Can anyone tell me how I can get the file path?
Try this:
--gets file path of selected song
tell application "iTunes"
set songLocation to get location of selection
end tell
return songLocation
--gets file path of currently playing song
tell application "iTunes"
set songLocation to get location of current track
end tell
return songLocation
If it's not available through AppleScript, your best bet will be to open and parse the iTunes plist file. If you only need static information, you're golden. If you need dynamic information (for example, info on the track that's currently playing), you'll want to get the track's persistent ID through AppleScript, then parse the plist file and lookup any info you need (including location, which has the full path).
For parsing that plist XML file (in ~/Music/iTunes/iTunes Music Library.xml), you can use ruby or python or any other language you like. If you like python, try this library:
http://docs.python.org/library/plistlib.html
If you're going to use Python instead of AppleScript and don't want to parse the plist XML file, you can get the file path through the COM API.
import win32com.client
iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
currentTrack = win32com.client.CastTo(iTunes.CurrentTrack,"IITFileOrCDTrack")
print currentTrack.Location
Related
I'd like to export all of my favorited photos/videos on a weekly basis to a zip file or directory. Is there an apple script/automator workflow that can do this that I can schedule to run?
Thanks for your response, I've tried what you suggested but get this error (please note that automator is automatically changing the text "using originals true" to "with using originals":
If we open up the scripting dictionary for Photos and search for “favorite”, we see the following:
There is an album object available from the root Photos application that contains all of your favorited items. Perfect! Now we need to export them… dictionary, what do you have to say on the matter?
There is a command in Photos that exports specified media items to a filesystem location. That's exactly what we need! So, so far, we have this in our script window:
tell app "Photos"
export every media item in favorites album to [export location here] ¬
using originals true
end
Obviously, you should replace [export location here] with the location you want to export to (via a file or POSIX file specifier). If you want to compress them into a zip file now, that should be pretty easy, too. In this case, since this functionality isn't provided by any preinstalled system application (that I am aware of), we can outsource the job to a command line utility called zip:
set quoted_out_location to quoted form of POSIX path of [export location here]
do shell script "zip -r " & quoted_out_location & space & quoted_out_location
And that's it! From there you can move the resulting zip file wherever you need using System Events or Finder or whatever you please, and delete the intermediate folder if you want. If this needs to run automatically on a regular basis, the easiest option by far is to embed the script in an Automator Calendar Alarm workflow, and attach it to a recurring calendar event. That's not too hard to find with a quick Google search, and this answer is long enough already.
Background
I am working on an AppleScript application (ASOBJ) through Xcode and frequently access (update/retrieve) various entries from a .plist file that I have created.
Whenever I need to access the data, I use the following command:
set thePropertyListFilePath to POSIX path of (path to resource "Data.plist")
tell application "System Events"
tell property list file thePropertyListFilePath
set myAge to value of property list item "userAge" as text
end tell
end tell
The problem
My project is in development and file names / paths change all too frequently.
I want to make a property thePropertyListFilePath : [...] at the top of my project to be able to change the path and name of the file in one place, rather than multiple.
I would like to use property over set as it just works better in my case.
At the top of my code:
property thePropertyListFilePath : POSIX path of (path to resource "Data.plist")
Does not compile because:
Error: Resource not found.
Analysis
I know for a fact that the resource Data.plist is where it is supposed to be and the paths correct because it works fine from inside my top code snippet.
Leaving me with the question:
How do I set a property to a file path?
Any insight into why this error is thrown and how to correctly set the property to this (relative) file path is greatly appreciated. I am aware of this question, but it does not present an applicable solution. Cheers.
Edit
I now seem to be getting a related error:
System Events got an error: 'This
Mac:Users:myuser:Library:Developer:Xcode:DerivedData:ProjectName:Build:Products:Debug:ProjectName.app:Contents:Resources:Data.plist'
is not a property list file. (error -1728)
Some validation needs to take place before app has launched so I wrote my code in the applicationWillFinishLaunching_ section - this shouldn't present any problems.
I know that the file is there and a .plist format so I am thinking it could simply be the tell ... statements around it.
In the past, I used set currentVersion to (current application's class "NSBundle"'s mainBundle()'s objectForInfoDictionaryKey:"CFBundleShortVersionString") as text to access the version number from the Info.plist in one go. Is it possible to use a similar one-liner without the tell statements to retrieve userAge from my own plist file?
Don't do that. Never declare a property with a relative path. The value will set at compile time and will never change. That makes the benefit of the relative path useless.
Declare the property as empty string
property thePropertyListFilePath : ""
and set it in applicationDidFinishLaunching
on applicationDidFinishLaunching_(aNotification)
set thePropertyListFilePath to POSIX path of (path to resource "Data.plist")
end applicationWillFinishLaunching_
However as you are developing AppleScriptObjC I recommend the Cocoa way (longer but more efficient)
set thePropertyListFilePath to (current application's NSBundle's mainBundle()'s pathForResource_ofType_("Data", "plist")) as text
I am trying to write an Apple Script for Sketch.app (com.bohemiancoding.sketch3). What i want to do is, create some image file that can be rendered in browser from Sketch document.
And when i open Sketch.app dictionary in Script Editior i see
saveable file format enum
Sketch : The native Sketch 2 file format
PDF : Portable Document Format
TIFF : Tagged Image File Format
So i thought about generating TIFF using following script, but it did not work
tell application "Sketch"
set curdoc to document 0
save curdoc in "/Users/mirza/Downloads/mew2" as TIFF
end tell
I can create sketch copies in .sketch format with save command but not PDF or TIFF. Does sketch supports PDF and TIFF using apple script?
Or is there any other way around for that.
Update
I change the path to apple script format and set document index to 1. Now script looks like this
set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
curdoc to document 1
save curdoc in thisFilePath as TIFF -- Tried with quotes as well, gives same error
end tell
But when i run the script i got the following error
Result:
error "Sketch got an error: Can’t continue curdoc." number -1708
Update 2
Fixed typo
set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
set curdoc to document 1
log (path of curdoc)
save curdoc in thisFilePath as "TIFF"
end tell
But when i run the script i got the following error
Result:
error "Sketch got an error: The document cannot be exported to the \"TIFF\" format." number -50
There are a number of things wrong with your code, but, to start with, you're going to find it hard to get definitive answers using software that isn't available anymore. Sketch has been at version 3 for a while now, and the AppleScript dictionary has probably changed.
That being said, here are some thoughts about your code:
If that is what the Sketch 2 AS dictionary reads, then the AS functionality has changed in v3.
I'd like to help, but I can't find v2 anywhere, so I can only do this in the dark.
set thisFilePath to choose file name--use this to select a new file;
------- a Mac AppleScript path is returned (a file specification,
------- actually, which is different from a string or alias
------- (but an alias is kind of like a file spec)
tell application "Sketch"
set curdoc to document 1--not zero-based; 1 is frontmost doc
save curdoc in thisFilePath as "TIFF"--*this is a guess
end tell
So, I don't know what that last save line will do, but it might work. In Sketch 3, "TIFF" format isn't allowed in saving, but it does have an as parameter as part of the save, which is supposed to be paired with a text string representing the format (like "TIFF", above). Sketch 2 seems to have a different scheme (the parameter with as is not a string). If I save without the as parameter in Sketch 3, it saves in Sketch's native format. So you might try this without quotes (like you have). I'm just doing what the v3 dictionary tells me to do.
Here are a couple of solutions and tips:
document 1 should work to reference the frontmost document;
If you want for some reason to write out your path using POSIX
(like you've done), you can use
POSIX file "/Users/mirza/Downloads/mew2"
to return an AppleScript's Mac-style path, which is of this form:
"yourHardDriveName:Users:mirza:Downloads:new2"
You can also get what I have here as "yourHardDriveHame:" by doing
tell application "Finder" to set sDr to startup disk as string
then concat by doing
sDr & "Users:mirza:Downloads:new2"
You can also do
tell application "Finder" to set myHome to home as string
which should return the Mac-style path to the home folder. (And yes, there are other paths Finder allows you to get, too).
There's some stuff to play with.
Some Background: I've used Skim with BibDesk for a while now for reading and annotating scientific journal articles. Recently, I purchased an android tablet and would like to use it to read and annotate .pdfs as well. I use the reference library Eratosthenes with ezPDF Reader and sync all files through Dropbox. The issue I'm having is that Skim stores annotations as extended attribute files by default, which are not accessible to other devices through Dropbox. I've worked around this problem by saving the annotations as .fdf files and then linking the .fdf files to the citation entry in BibDesk. ezPDF reader can import .fdf files as annotations, and if the annotation file is linked to the BibDesk entry, both the .pdf and .fdf can be easily downloaded to the tablet without needing to sync the entire Dropbox folder full of hundreds of references.
I'd like to write an applescript that does this automatically, but am very new to applescript and am having a hard time getting started. I've written the following script to be executed when "command-s" is pressed while in skim:
tell application "Skim"
set docPath to path of front document
set notesPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in notesPath as "Notes as FDF"
end tell
This essentially saves the current document while simultaneously exporting an .fdf file. Within the same script, I would like to link the .fdf file to the appropriate citation entry in BibDesk. This would involve:
determining the citation entry name associated with the .pdf (maybe search through entries to locate one linked with the front document)
check to see if the .fdf file is already linked to it
if not, attach .fdf file
I haven't been able to find someone who's done something similar, and really can't get past the first step. I tried writing something basic (assume citation entry is highlighted, assume .fdf file is not linked), which produces no results:
tell application "BibDesk"
set thePub to selection
tell thePub
set theFieldName to "Local-URL-2"
set value of field theFieldName to fdfPath
end tell
end tell
Is anyone familiar with Bibdesk applescripts able to help me with the second part of this code?
Thank you very much in advance.
I'm not too familiar with scripting BibDesk, but I've been poking around a little. Heavily commented to help guide you:
set theFieldName to "Local-URL-2"--generally better not to put
--something like this in a tell block if it isn't necessary
tell application "BibDesk"
--as you have it, you are simply putting the selection class
--into a variable. here we reference the specific selection
--object of the document object:
set thePubSel to selection of document 1
--and, since this returns a *list* of pubs, I'm grabbing just the first item
--(but a loop iterating through every publication in the selection perhaps better)
set thePub to item 1 of thePubSel
tell thePub
set value of field theFieldName of it to fdfPath
end tell
end tell
The following code answers my initial question satisfactorily. The first section reiterates the saving and exporting commands. The second section locates the citation entry containing the linked .pdf file (front document in Skim). The third section attaches (links) the .fdf file to the citation entry (thanks to CRGreen for some help here).
--save and export .fdf file
tell application "Skim"
set docPath to path of front document
set fdfPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in fdfPath as "Notes as FDF"
end tell
--search for relevant citation entry
tell document 1 of application "BibDesk"
--sort all publications in library by Cite Key
set thePubs to (sort (get publications) by "Cite Key")
-check each publication individually (surely this is not the most efficient way to do this)
repeat with aPub in thePubs
--check to see if the .pdf is in the citation entry
tell aPub
if linked files contains (POSIX file docPath) then
set thePub to aPub
--once the citation is found, exit loop
exit repeat
end if
end tell
end repeat
--link the .fdf file to the citation entry (if it isn't already)
tell thePub
--if the fdf file exists in the linked file, do nothing
if linked files does not contain (POSIX file fdfPath) then
add (POSIX file fdfPath) to end of linked files
end if
end tell
end tell
I'm assuming that there is a better way to search for the citation entry with the associated .pdf file (maybe using the applescript search command?). This solution works for me, but if you know of a more elegant way to solve this problem, feel free to mention it.
To map the script to a keyboard shortcut ("command-s" is convenient), see here (the menu title is the name of your script). The script needs to first be saved to the ~/Library/Application Support/Skim/Scripts directory.
I'm just learning applescript, and I realize that this may have been a very trivial exercise for most, but perhaps it will help out another beginner.
So, i'm working on a script that copies the home folder to a mounted shared drive folder. But i'm getting the following error:
"Handler can't handle objects of this class number -10010"
This is the code I came up with following the example of other code i've seen on here. I'm guessing that it is the way i'm telling finder to duplicate.
set vserver to ("/Volumes/sharedfolder")
set source to ("/Users/user")
tell application "Finder"
duplicate source to vserver
end tell
How else can I write this?
I've also tried running a boolean test to see if Finder saw the shared folder or my home folder and it retured false. (but only one false when it should have returned two)
tell application "Finder"
setaBoolean1 to get (exists vserver)
setaBoolean1 to get (exists source)
end tell
set vserver to ("/Volumes/sharedfolder")
The line above sets the variable vserver to a string object consisting of "/Volumes/sharedfolder". Likewise, the set source to "/Users/user" line sets source to a string object containing "/Users/user". Note that strings are not what the Finder is expecting when you're telling it to duplicate items.
The tell app Finder line is basically trying to tell the Finder to duplicate one string into another string, which it doesn't know how to do (hence the Handler can't handle objects of this class message).
What you need to do is to, instead of creating strings, create some sort of file system reference to those folders, so that the Finder knows how to deal with them.
There are numerous ways to do this, but the method I found that works (which uses the same POSIX style path format) is the following:
set vserver to POSIX file "/Volumes/sharedfolder"
set source to POSIX file "/Users/user"
tell application "Finder"
duplicate source to vserver
end tell