Check if two files are the same in Applescript - 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

Related

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

Applescript to extract subject line

Total n00b here, definitely not a programmer. Would love some assistance with an applescript. I'm basically trying to extract a subject line from emails in a particular folder underneath my inbox. I need it to yank out the subject line, look for some numbers (ex. 123456) and pull out the last 4 digits. Then put that into a text file. Below is what I have so far, but it's not working. I'm not getting any output at all. Any guidance would be greatly appreciated!
tell application "Microsoft Outlook"
set theAccount to exchange account “my account"
set topFolder to folder "Inbox"
set subFolder to folder “Stuff"
set theMessages to messages of subFolder
set folderPath to ((path to home folder from user domain as string) & “emails")
repeat with aMessage in theMessages
my SetSubject(subject of aMessage)
end repeat
end tell
on SetSubject(theSubject)
tell application "Microsoft Outlook"
try
save theSubject in ((path to home folder from user domain as string) & “emails" & “numbers.txt" as string)
end try
end tell
end SetSubject
end
I don't know what your criteria are to filter the subjects.
This is an example to write the last 4 characters of all subjects of the messages in the specified mailbox into a file numbers.txt in folder emails in the home folder, one subject per line.
If an error occurs – for example the number of characters in the subject is less than 4 – the file is closed reliably and the script aborts.
set numbersFile to (path to home folder as text) & "emails:numbers.txt"
tell application "Microsoft Outlook"
set theSubjects to subject of messages of folder "Stuff" of folder "Inbox" of exchange account "my account"
end tell
try
set fileDescriptor to open for access file numbersFile with write permission
repeat with aSubject in theSubjects
write (text -4 thru -1 of aSubject & return) to fileDescriptor starting at eof
end repeat
close access fileDescriptor
on error
try
close access file numbersFile
end try
end try

Can't get POSIX Path of given application in Applescript

I've written some Applescript to create a list of paths of all
applications running in the dock:
set appsRunning to []
tell application "System Events"
repeat with p in every application process
if background only of p is false then
set appsRunning to appsRunning & POSIX path of (path to application p)
end if
end repeat
end tell
But when it runs I get
the error - "Can't make application into type constant" and it
highlights path to application p.
I don't understand why this happens because when I run
set q to POSIX path of (path to application "Finder") -- or any other application
I get no error whatsoever and I see
"/System/Library/CoreServices/Finder.app/" returned in the Results
field.
How can I get this to work?
P.S. For my purposes it is essential that I get the path - the
application name simply won't do. (This is because when I get the name
of the application process, some of my applications which are SSBs
made using Fluid return "FluidApp" as their
name instead of "Gmail" or "Tumblr" or whatever site it is that
I've made into an application. I need to distinguish between these and
that only happens when I get the path.)
Any help would be appreciated! Thanks.
Update: I used an amended version of the first suggestion in #vadian's answer to solve my problem:
set appsRunning to {}
tell application "System Events"
repeat with aProcess in (get application file of every application process whose background only is false)
set appsRunning to appsRunning & POSIX path of aProcess
end repeat
end tell
The element application process of System Events has a property application file, which you can get the POSIX path directly from.
set appsRunning to {}
tell application "System Events"
repeat with aProcess in (get every application process whose background only is false)
set end of appsRunning to POSIX path of application file of aProcess
end repeat
end tell
or easier
tell application "System Events"
set appsRunning to POSIX path of application file of every application process whose background only is false
end tell
additional here a solution which excludes the Finder because it runs all the time and the path is fixed
tell application "System Events"
set appsRunning to POSIX path of application file of every application process whose background only is false and name is not "Finder"
end tell
set end of appsRunning to "/System/Library/CoreServices/Finder.app"
another solution using your original approach
set appsRunning to {}
tell application "System Events"
set applicationNames to get name of every application process whose background only is false
end tell
repeat with aName in applicationNames
set end of appsRunning to POSIX path of (path to application aName)
end repeat
and last but not least the AppleScriptObjC version (Mavericks and higher, in Mavericks only in a script library)
set appsRunning to (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
Though the method launchedApplications of NSWorkspace is deprecated, it works in Yosemite
to use the AppleScriptObjC in a script library save this code
use framework "Foundation"
on launchedApplications()
return (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
end launchedApplications
as script bundle (in Mavericks you have to check "AppleScript/Objective-C library" in the side bar of the script) in ~/Library/Script Libraries. Create the folder if it doesn't exist.
Now you can call the script library from a normal script file (the script library is named "NSWorkspace.scptd")
use script "NSWorkspace"
set appsRunning to launchedApplications() of script "NSWorkspace"

Filling in gaps in itunes

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

Applescript that filters the subject line of emails in Inbox

I am trying to write a script that does the following job: it goes through all of the emails in the mailbox, finds the ones that have the word "French" in their subject line and then copies all the subject lines of those emails in a text file. Here is what I came up with
tell application "TextEdit"
make new document
end tell
tell application "Mail"
tell the mailbox "Inbox" of account "tigeresque#gmail.com"
set numm to count of messages
repeat with kk from 1 to numm
set wordsub to subject of the message kk
tell application "TextEdit"
if "French" is in wordsub then
set paragraph kk of front document to wordsub & return
end if
end tell
end repeat
end tell
end tell
Unfortunately, I keep receiving the error
"TextEdit got an error: The index of the event is too large to be valid."
and I have already spent a couple of hours trying to fix it without much success. Could you please take a look at my code and see what is wrong with it?
Your main problem is that the number of paragraphs in TextEdit and the number of email messages have nothing to do with each other, so if you're counting on the number of messages then TextEdit will not understand it. For example you may have 50 messages but TextEdit does not have 50 paragraphs so it errors. As such we just use a separate counter for TextEdit.
I made other changes too. I often see errors happen by having one "tell application" block of code inside another... so I separated them. Also notice that the only code inside of any "tell application" block is only what is necessary for that application to handle. This too avoids errors. These are good habits to have when programming.
Therefore give this a try...
set searchWord to "French"
set emailAddress to "tigeresque#gmail.com"
tell application "Mail"
set theSubjects to subject of messages of mailbox "INBOX" of account emailAddress
end tell
set paraCounter to 1
repeat with i from 1 to count of theSubjects
set thisSubject to item i of theSubjects
if thisSubject contains searchWord then
tell application "TextEdit"
set paragraph paraCounter of front document to thisSubject & return
end tell
set paraCounter to paraCounter + 1
end if
end repeat

Resources