BibDesk script to link Skim annotations to citation entry - applescript

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.

Related

Convert a PDF file into CSV, Excel or Numbers format using Applescript

Every month I get my bank balance which it is basically just a table. In order to get a better overview of my expenses I would like to export this list into a Numbers (iWork) document. My wish would like to use Applescript so can automate this process :)
Does anyone knows how can I convert a PDF (text-type) list into CSV, Excel or Numbers?
If you've tried any code, it would be good to see. Also, it's hard to know what the results might be without seeing how the type of pdf you're working with goes through this kind of process. With that in mind, here's the kind of process I'm talking about...
I would suggest downloading and using the beautifully script-able Skim pdf app (http://skim-app.sourceforge.net/). Preview is too limited. The basic Adobe reader probably has adequate functionality, but, well, I'm a Skim fan.
A CSV file is basically just text, so this should work, but again, not knowing how your text is formatted I cannot be sure of the results. I'd be happy to make edits if you share examples. The following worked well with a pure text pdf:
tell application "Finder" to set dt to desktop as string
tell application "Skim"
set docName to name of document 1
set baseName to text 1 thru ((offset of "." in docName) - 1) of docName
set docText to text of document 1
set filePath to (dt & baseName & ".csv")
set myFile to open for access filePath with write permission
write docText to myFile --as «class utf8»--depending on results, you may need this last part. test
close access myFile
end tell
tell application "Numbers"
activate -- I got weird buggy results not activating (or at least launching) first.
open alias filePath
end tell

Apple script for Sketch App

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.

Applescript/automator : move all the files listed in a document

I'm completely rebuilding a shared iTunes library and this needs to be team work.
I found a way to work the XML database in Google Drive so that we can all edit the track list simultaneously (>7500 entries). The spreadsheet contains for every song the path to the corresponding file.
Now I need a script to move the tracks listed in that spreadsheet to a common folder, so I can separate the songs we decided to keep from the ones we don't want anymore.
The blueprint I imagined for the code is basically :
Get the paths list (txt, csv, etc. doesn't matter) and store it as
an array.
Rotate through that array and select+move to a common folder each file pointed by the paths.
I'm not expecting any ready-to-use solution, but I would really appreciate some tips or pieces of advice that could make me spare a lot of time.
I also have to admit I have limited knowledge in Mac OS X programming (more used to web and windows environments) and have no experience in Applescripts.
However, I feel that what I'm trying to achieve is pretty straightforward and could help other people as well.
there are just 2 items not clear in your request :
1) what is the file type in which file paths are stored ? I assumed it is a text file
2) which format the paths have ? is it Unix format (like HD/Users/My_User/Desktop/My_Song), or is is a Finder format (like HD:Users:My_User:Desktop:My_Song). I assume it is a finder format
then script bellow asks you to select the text file, read it, ask you to select destination folder and move every file described in text file to the destination folder.
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to choose folder with prompt "Select your destination folder"
set List_files to paragraphs of (read TextFile)
move List_files to My_Folder
end tell
the "move" can be changed to a "copy", if required
Thank you so much to both of you for your answers.
Regarding pbell's questions :
1. I did not specify the format of the file containing the paths because I wasn't sure which ones the script could handle. Now I will make sure it is in a txt format.
2. The paths are actually il a Unix format, but seeing your example it looks pretty easy to parse. Can I just replace every "/" with a ":" ..?
I will try your code this afternoon. Again thanks a lot for caring and for sharing your time and knowledge.
Terence
Works like a charm !
Thank you very much pbell, your code works perfectly. I just had to make a txt file with Finder-formatted paths like so :
Macintosh HD:Users:FirstnameSurname:Desktop:Music:November-7:Season 3:04 Parasite.mp3
Macintosh HD:Users:FirstnameSurname:Desktop:Music:November-7:Season 3:05 Nowhere.mp3
Macintosh HD:Users:FirstnameSurname:Desktop:Music:November-7:Season 3:06 Amber Light.mp3
We spared so much time thanks to you.
Have a great day,
Terence

Apple script to delete files with custom label (mavericks)

I'm trying to create a simple script to delete files based on a custom label I've already assign.
I'm currently trying to limit the search for the script to a test folder, but ultimately I want the script to search in all the user folder and get all the files from several different locations. I may need authentication for the process.
But so far I have this
tell application "Finder" delete (every item of folder
"/users/ro/documents/Erase test" whose label is "test") end tell
and I get this error
error "Finder got an error: Can’t get folder
\"/users/ro/documents/Erase test\"." number -1728 from folder
"/users/ro/documents/Erase test"
As I said I don't really know much about scripts, so I don't know all the terms but I hope someone can point me in the right direction.
Saw this late.
Tested this on 10.6.8 and will jump on a Mavericks machine to test, but this should work:
set f to choose folder
tell application "Finder"
delete (every item of f whose label index is 1)
end tell
A few notes about your attempt:
1) AppleScript doesn't 'natively' understand POSIX paths (but coercion to/from is possible), so (as I have it) "choose folder" returns what is known as an alias (not to be confused with a string -- but again, coercions to/from strings/aliases are simple).
2) note that the label is recognized as "label index", which is an integer.
3) you could/should test by taking out "delete" in that line to return a list of those items.
[edit] yes, this is fine on Mavericks.

batch renaming items in subfolders with apple script

I have tried all day now to do the following:
I have a folder with 99 subfolders. each subfolder has a pdf file inside and they all have to be renamed to carry the same name. now they are named with continuous numbers.
I have been doing all my stuff with the automator as I am a novice to scripting. but i kinda don't like giving up on a problem. googling did not bring any good solutions.
i have tried to make sense of example scripts and amend them, but had no success.
a short try was:
tell application "Finder"
set selected to selection
open selected
get files of folders of selected
set name of files of folders to "anschreiben"
end tell
I have also tried:
tell application "Finder"
set selected to selection
open selected
set mlist to every folder of selected
set current_folder to first item of selected
set xxx to first item of current_folder
set name of xxx to "yyy"
repeat with this_folder in mlist
open current_folder
set item 1 to item of current_folder
end repeat
end tell
, but this renamed the first folder to yyy and produced and error that the file name is already given (because it renames the subfolders, not the files inside)
SO...how do I get on level deeper and rename all the files in the subfolders??
Thanks guys, I know it's probably easy for you.
Look for "entire contents"
set newName to "New.pdf"
tell application "Finder"
set myFiles to every file of (entire contents of (first item of (selection as alias list)))
repeat with aFile in myFiles
set aFile's name to newName
end repeat
end tell
it is done! Thanks to you and one or two other pros I now have a beautiful bulk mailing script routine using automator, a bash line and (mainly) applescript. I use it for job applications but you can use it for any case where you want individualised bulk emailing with Mail, MS Word and any given list of contacts in Excel (or Address Book for that matter). For the sake of being complete I will add all necessary steps. with any given list of x names, email addresses, personal addresses you can generate x subfolders, containing x personalized letters and not-personalized documents. once you start the last script and select the folder you can watch mail sending them all out, addressing the person by name and attaching the right personalized letter (you were involved in this part, adayzone!)! It corrects for foreign name spelling that is rendered differently in the email address. It works best for email addresses using the last name before the "#" and can now ignore the first name if it is set in front of the last name (i.e. firstname.lastname#company.com). Thank you all very much for the assistance! this was great team effort.
I shall post it as soon as I am home, should I add it in here or in another forum (for sharing code)?

Resources