Exporting Attachments from Evernote using AppleScript - applescript

I am having significant issues creating a very simple AppleScript to export attachments from an Evernote note. I have used the following code several times and it seems to work, but in actuality it fails silently, even though the event log shows a correct response.
I have heard that Evernote's recent versions have significant AppleScript support issues. If someone out there is an Evernote and AppleScript "expert", I would also greatly appreciate a review of my script to make certain I'm not forgetting anything.
I am using Mac OS X 10.8.2 and the Mac App Store version of Evernote 5.0.5 (400805).
Code:
set ExportPath to "LegatusHD1:Users:me:Downloads:Test"
set theAttachments to {}
set selectedItems to {}
tell application "Evernote"
set selectedItems to selection
repeat with selectedItem in selectedItems
set theAttachments to (attachments of selectedItem)
repeat with theAttachment in theAttachments
set theFilename to (ExportPath & ":" & theAttachment's filename)
write theAttachment to theFilename
set theFilename to ""
end repeat
end repeat
end tell

Apple has introduced sandboxing for apps from the AppStore recently. So depending on your OSX version, this might be the reason why it fails silently - since the Evernote app is not able to access the file system outside its own folder structure. You can try setting the export path to be something within the Evernote folder.

This question is old but I just tripped over it looking for an answer which I can now provide:
FYI I am running: OSX 10.9.1 (Mavericks) and Evernote 5.4.4 installed via App Store.
I can confirm that Mustafa was right, using the App Store version of the app it is only possible to write to files within the sandbox (/Users/{username}/Library/Containers/com.evernote.Evernote/)
Short answer
Either install the app from the Evernote website or if you want to use the one in the App Store:
Using the above script change the export path to something inside the sandbox e.g.:
set ExportPath to "Users:{username}:Library:Containers:com.evernote.Evernote:Data:attachment_export"
Remember to make the attachment_export folder first, and replace the {username} with your username
A note on debugging
Unfortunately applescript editor doesn't show any errors when you try to write outside the sandbox. You can however view the error in the Console application (found in Applications -> Utilities). In Console enter 'sandbox' in the search box (top right hand corner) and at the bottom of the window you will see something like this:
1/29/14 10:22:29.426 sandboxd[310]: ([4433]) Evernote(4433) deny file-write-create /Users/{username}/Desktop/Evernote Snapshot 20140128 134741.jpg

Related

MacOS Sierra & SMB & Synology NAS & Applescript crashes Finder

I've discovered a bug that I don't know how to fix. So I have an Applescript that tells the mac to highlight a certain file in an already opened Finder window connected to a shared folder on a Synology NAS server. Here's the Applescript:
set theFile to "West Office Files:_Scan Inbox:LAR:1002.pdf"
tell application "Finder"
reveal document file theFile
end tell
An error pops up saying that Finder got an error: document file "West Office Files:_Scan Inbox:LAR:1002.pdf" doesn't understand the "reveal" message. I have also tried using "select" instead of "reveal" and the problem persists. However, I just discovered it doesn't throw an error the first time the script is ran. But if I run it again to select a different file (but in same folder location) it then throws the error.
Then strange things begin to happen in Finder. Can't click on a finder window showing files, can't close any windows. Sometimes Finder will start popping up what I call "shadow boxes" which is the drop shadow of a window, but the window is invisible. Finder won't correct itself and the only fix I've found is to quickly log out the user to the Mac login screen and log back in. If I don't Finder will get worse and ultimately requires a force restart.
This is ONLY happening with MacOS Sierra (any sub version) and when connected to a Synology NAS server (any version DSM 6+) via SMB. AFP works fine, pre-Sierra Mac OSes work fine, and even when connected to an old Mac OS (Lion) Server via SMB or AFP there is no problem.
Anyone experiencing this? Any ideas for a solution?
Untested (im on mobile) but you need to transform osx path to posix file:
set theFile to POSIX file (POSIX path of "West Office Files:_Scan Inbox:LAR:1002.pdf")
tell application "Finder" to reveal theFile
With help from Pat_Morita I found a solution to the problem. Am posting it here to help others who use Synology NAS servers and Macs.
set theFile to POSIX file (POSIX path of "/Volumes/West Office Files/_Scan Inbox/LAR/1002.pdf") as alias
tell application "Finder"
set selection to {}
select theFile
end tell
Where the error was occurring was specifically when the AppleScript would tell the Finder to highlight a file and there was a file already highlighted. In MacOS Sierra it would throw an error in the Apple Event right when it moves away from the currently selected file. So adding in the "set selection to {}" has Finder deselect what is highlighted before selecting the next file. I also went with using POSIX for the file path by recommendation from this forum.
UPDATE ---
False alarm on finding a fix, well... sorta.
The error is still thrown and seems to only be happening when a Finder window is in Cover Flow view. All the other views work fine. Also, I discovered the bug is happening when MacOS Sierra is connected via AFP as well, not just SMB. I changed the AppleScript a bit:
set theFile to "West Office Files:_Scan Inbox:LAR:1002.pdf" as alias
tell application "Finder"
set selection to {}
select file theFile
end tell
Learned that Finder doesn't really care if the file path is set up via POSIX or not. Also added "file" to the select line.

How to modify MacOS Dock shortcuts/hotkeys?

I want to modify/change/add MacOS Dock shortcuts/hotkeys.
e.g., of a shortcut that is available by default:
Option-Click on Dock app icon of an app that is not currently open = Hide the currently active app and then Open the app that was clicked
(from: https://support.apple.com/kb/PH21922?locale=en_US)
One very specific ability that I want:
Open and then Hide an app
Shift-Click on Dock app icon = Open and then Hide that app
(or use another easy modifier-key-combo with the click)
I am aware of the bash command open -a App --hide (e.g., open -a TextEdit --hide). I want to implement this exact functionality with a convenient Dock shortcut like the one mentioned above. If you're wondering "why?": sometimes I just want to open an app because I know that I will need it soon, but I'm still busy with another app, so just open this second app and then immediately hide it so it doesn't get in my way while I'm still busy with that first app.
How do I do this?
You can't.
These keyboard bindings are built into the Dock application, and cannot be modified.
The Mac utility program, Keyboard Maestro, does what you've asked, with shortcut keys, without using the Dock:
open, then immediately hide/minimize
option+open
I came up with a related solution: (in case anyone is interested)
an AppleScript App that presents a pick list
Method:
create a plain text document containing a list of the apps you want to handle (use correct name, no path, no extension, one name per line, no commas)
open 'Script Editor' (/Applications/Utilities)
copy-pasta the following code (and edit the first code line for the path to your text file from first step)
set apps_file to ("path:apps_list.txt")
set apps_list to paragraphs of (read file apps_file)
set apps_pick to choose from list apps_list with prompt "Select one or more apps." with multiple selections allowed
if result is false then return
set path_base to "Macintosh HD:Applications:"
set path_msft to path_base & "Microsoft Office 2011:"
set path_utly to path_base & "Utilities:"
set spec_msft to "Microsoft"
set spec_utly to "Activity Monitor, Terminal"
repeat with apps_this in apps_pick
if apps_this contains spec_msft
set path_this to path_msft
else if apps_this is in spec_utly
set path_this to path_utly
else
set path_this to path_base
end if
set apps_open to path_this & apps_this & ".app"
run application apps_open
end repeat
return
i. navigate menu 'File -> Export...'; ii. use the option 'File Format: Application'; iii. Save; (put the resulting app in your Dock)
Its not quite as convenient as I hoped, but, not too shabby.

xcode 4.6 applescript open text file for reading

I am trying to incorporate my applescript into xcode. the script works normally with applescript but not ran in xcode. I am trying to open the file for reading. here is the code
set Location to "US"
set DriverFile to "/Volumes/MacPrintDrivers/" & Location & "DriverInstall.txt"
set DriverInstallFile to POSIX file DriverFile
open for access DriverInstallFile
i have confirmed the file exists and i can display the contents. i can't seem to read this way in Xcode. the error i get is
«script» doesn’t understand the «event rdwropen» message. (error -1708)
ASOC (AppleScript Objective-C) has some problems with scripting addition commands (such as open for access). Sometimes you can work around this by saying tell current application to, e.g. tell current application to open for access. But for further details I suggest you get Shane Stanley's book: http://www.macosxautomation.com/applescript/apps/ He has explored this in great depth.

OS X kernel extension graphical uninstaller

XCode doesn't include uninstallation options for their packager. Generally users simply drag the app to the trash if they want to uninstall it - most apps are self contained in the app package.
However, I have a kernel extension which must be uninstalled using a few simple command lines. There is a script that works, but I am required to provide a graphical uninstaller.
I don't expect there's a plug-n-play example code out there that provides a way to run the script while showing a progress bar, but I'm hoping someone has dealt with this or has a few pointers on how to accomplish this quickly and easily
The script is only two lines with no feedback, so we can execute the commands in the app, as long as we can easily request and use root permissions securely (ie, let OS X handle the permissions - we merely ask for OS X to give them to us which should cause it to ask the user for them similar to how it happens with the package installer) inside the app.
There's a reasonably good approach using a Cocoa-Applescript project in xcode to run a shell script here:
http://www.mactech.com/articles/mactech/Vol.22/22.08/GUI-upyourScript/index.html
It covers using a progress bar, handling errors, and getting the correct root permissions to run the shell script.
Unfortunately it's a bit long to cut and paste here, but the general steps are:
Create new xcode project of type Cocoa-Applescript app
Create and test the intended shell script, add it to your project
Edit the MainMenu.nib to add and name a button(theObject) and progress bar(pMainProgress), then edit title and other aspects of the ui to taste
Tie the button to the applescript in the project (in the Inspector with the button selected check the action box and put in myprojectname.applescript)
Edit the applescript to something akin to the following:
on clicked theObject
-- setup
set myPath to POSIX path of (path to me) as string
-- Start progress bar
set uses threaded animation of progress indicator "pMainProgress" of window "wMain" to true
tell progress indicator "pMainProgress" of window "wMain" to start
-- Do it!
try
do shell script quoted form of myPath & "Contents/Resources/backup.sh" with administrator privileges
on error number 255
display dialog "Sorry, an error occurred. I can't make the copy."
end try
-- Stop progress bar
tell progress indicator "pMainProgress" of window "wMain" to stop
set uses threaded animation of progress indicator "pMainProgress" of window "wMain" to false
end clicked
You can further customize the app (name, for instance) and add text boxes to the window to alert the user what step is happening if you're running multiple scripts (put set the contents of text field "efStatus" of window "wMain" to "Copying files..." in your script after adding a text field to the ui with the name "efStatus")

Create AppleScript for a program that isn't installed on the current computer

I'm trying to make two copies of an AppleScript, one that works for Entourage and one for out Outlook. I only have Entourage installed on the current computer.
According to the info on Microsoft's site, both applications have the same library of AppleScript commands, and I should be able to simply change the application name referenced within the script.
Changing:
Tell application "Microsoft Entourage"
to
Tell application "Microsoft Outlook"
Prevents me from saving the script because I don't have outlook installed on this computer. Is there any way around this? Do I need to use a text editor to edit the actual script file and change it there?
Thanks!
The following work-around may do the trick. On the computer where Entourage is installed, a using terms directive will let you compile the script, even if Outlook is not installed:
set theApp to a reference to application "Microsoft Outlook"
using terms from application "Microsoft Entourage"
tell theApp
get version
...
end tell
end using terms from
Upon compiling and saving the script the AppleScript Editor will bug you about the missing Outlook application, but it will nevertheless produce a compiled AppleScript file (.scpt).
Applescript is a pre-complied file format, meaning that every time you click "Save" it runs through a series of steps to ensure the script will work, but just short of actually running through the script's logic. Part of those steps is to look for the application to see if it exists on the Mac.
In short, if you want to save the script as an Applescript, you need the target application installed, otherwise you can save the script as a text file and move the file over to the target Mac to save as an Applescript over there.
It should be possible to make one script that works with both Entourage and Outlook, without bugging you if one isn't found either when you compile or when you run. I don't have either Entourage or Outlook but it should work like this:
using terms from application "Microsoft Entourage"
script theScript
tell application "Finder" to try
set theApp to application file id "Entourage's Bundle ID" as text
on error
set theApp to application file id "Outlook's Bundle ID" as text
end try
tell application theApp
-- do stuff
end tell
end script
end using terms from
store script theScript in "MyScript.scpt"
"using terms from" is only relevant when compiling the script - it isn't needed when running, though for some reason you'll still get bugged if that app isn't found. So by wrapping it around a script object and then writing out that script to file, the resultant script will still run but won't contain "using terms from" and so won't bug the user.
For getting a reference to the right app, Finder can look for it by ID and simply error if it isn't found rather than bugging the user. You'll need to insert the proper ID's there, I don't know what they are.

Resources