applescript function to import a mailbox into Mail.app no longer functional in MacOS Mojave and above, yet no apparent error is returned - applescript

I have used the functionality to import mailboxes into Apple Mail in the past. Using Apple Script it is (in principle) easy to tell Mail to import a Mailbox. For some reason this does no longer work, eg the script runs without any feedback or error, but nothing happens. Is this something known, or am I overlooking anything basic?
Here is a minimal version of a script:
on run argv
tell application "Mail"
try
import Mail mailbox at "Macintosh HD:Users:baaden:exmh.mbox"
on error e number n
log "OOPs: " & e & " " & n
end try
end tell
end run
The core functionality that does no longer work is
tell application "Mail" to import Mail mailbox at "Macintosh HD:Users:baaden:exmh.mbox"
Of course the exmh.mbox file is a proper mailbox, and if I trigger the import manually through Apple Mail's menu item it does exactly what I expect. Any help or leads on how to understand what is going wrong here is much appreciated. Thanks.

Related

Applescript intermittently fails when moving selected email messages to folder

hope someone can tell me what I am doing wrong. I am an applescript newby, with help from other posts was able to use automator, applescript & keyboard shortcut to move selected messages to an outlook folder. Worked great. I transferred to another mac, works fine there. Then intermittently I get an error message on both macs:
The action "Run Applescript" encountered an error: "No messages selected. Select at least one message."1
I initially thought it was related to when I last opened up Outlook as it seemed to fix itself after a few minutes. Then problem comes back, no rhyme or reason I can perceive. Yes, I do in fact select a message first.
I made two copies of script, differences are folder names. Two different hotkeys attached, option+B for one folder option+P for another. Same error if I use hotkey or go to outlook/services then select the script
Any clue what I am doing wrong? thanks in advance for any insight!
Outlook for mac / O365 v: 16.62 (22061100)
osx big sur v: 11.6 (20G165)
here is script:
on run {input, parameters}
tell application "Microsoft Outlook"
activate
set msgSet to current messages
if msgSet = {} then
error "No messages selected. Select at least one message."
error -128
end if
set theMsg to item 1 of msgSet
set theAccount to account of theMsg
set archiveFolder to folder "Broker Correspondence" of theAccount
repeat with aMessage in msgSet
move aMessage to archiveFolder
end repeat
end tell
return input
end run

Exporting Attachments from Evernote using 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

How to compile AppleScript for application that is not installed

I have a script, which creates a new email message with default mail client on mac. The script contains a code, that relates to Microsoft Outlook if it finds that it was set as the default Mail application. The problem is that on macs, that Outlook wasn't installed this script even doesn't pass a compilation, because probably Apple Script cannot find Microsoft Outlook dictionary. What is the proper way to handle this problem?
Thanks!
Here's how you might handle it.
NOTE: in order for the script to not need compilation you must 1) save the script as an application. Applications do not need to compile on another machine. And 2) you use the "using terms from" claus in your script.
I added the do shell script stuff so you can check for Outlook prior to running the outlook code if you need it. I hope that helps.
set outlookExists to false
try
do shell script "osascript -e 'exists application \"Microsoft Outlook\"'"
set outlookExists to true
end try
if outlookExists then
using terms from application "Microsoft Outlook"
tell application "Microsoft Outlook"
-- do something
end tell
end using terms from
else
-- do something else
end if

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.

How do I debug AppleScript?

What tips and tricks do you have for debugging AppleScript? Is there a debugger? If not, what is the best way to insert "prints" to display the value of variables? Is there a way to "pretty print" more complicated data structures?
The latest versions of xcode will let you create an AppleScript application but breakpoints in applescript don't work since Apple has discontinued support for AppleScript debugging in xcode.
Fallback: for simple "printf style" debugging you could use:
display dialog "my variable: " & myVar
Script Debugger
XCode
Getting the properties of an object (see below) to understand why it fails, when run from script editor. You can also use the class word, to see what class a property is. The Dictionary for an app is a good starting point.
One technique that often would have helped me, (and that I still sometimes do) is to tell something to return their properties, like this:
tell application "TextEdit"
get properties
end tell
Log statements and Console.app, for debugging of runtime events. (further below). You can ofcourse turn debugging on and off by setting a property
Below is a techniuqe I use for tracking runtime errors, in applets, mail rules, and what have you. When it fails, the error number and message is logged into TestDrive.log, and can be found in the left margin of Console.app…
tell application "TextEdit"
try
set a to text 0 of its name
on error e number n
my logit("OOPs: " & e & " " & n, "TestDrive")
end try
end tell
to logit(log_string, log_file)
do shell script ¬
"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit
If you're building any amount of AppleScripts, ScriptDebugger is the best tool I can recommend. Having said that...
Xcode is a free option that can be used to develop AppleScripts and can step through code with the debugger. The ability is primarily included so you can build Cocoa applications with AppleScript Studio, but you could use it for any AppleScript development.
If you're looking for something simpler, you might check out Smile, which isn't really a debugger, but does offer features useful for debugging that aren't available in the standard Script Editor.
If a display dialog is too small you can use TextEdit to show big returns:
tell application "TextEdit"
activate
make new document
set text of document 1 to myResults
end tell
Source http://forums.macrumors.com/showthread.php?t=446171
To get the names of windows and other GUI elements and properties, I've found UI Browser invaluable. You can use it to inspect whatever you want to control with AppleScript to find the designations of the elements you want to control
Not free, but easily worth it for a serious developer.
Use the log command. Example:
log "Hello world!"
The output can then be seen in the "Messages" windows in the official editor.
Reference

Resources