Thursday, December 14, 2017

Using PlistBuddy when I want to open Emacs from a link in an HTML file on OS X

Previously, I have discussed How to open Emacs from a link in an HTML file on Mac OS X. This post is just to give a complete overview of how this can be done without to many manual tasks. My complete AppleScript program and its Makefile is available for download. It should only be used with your own modification.  It includes the usage of cvs to commit the changes in the bib files to my local cvs-server and the establishment of a VPN connection if I am not on my local university network (since my cvs-server is not available outside the university network). This part should be completely recoded for your usage.

I use BSD make to compile and install the software, and I use PlistBuddy to manipulate the Info.plist file of the AppleScript application. The focus of this post is how to modify the Info.plist file of the AppleScript application using PlistBuddy. A lot of resources for PlistBuddy is available online: A Simple PlistBuddy TutorialHow To Manage Plist Files With PlistBuddy, and OSX: Dealing with property list files.

In the previous post I explained the changes needed to be done in the Info.plist file of the application. The application is compiled using osacompile:

osacompile -o EmacsURL.app EmacsURL.applescript

The Info.plist is now located in the EmacsURL.app/Contents directory. To add the necessary properties in the Info.plist file we perform the following PlistBuddy commands (in the EmacsURL.app/Contents directory):

PlistBuddy -c 'Add :CFBundleIdentifier string com.apple.AppleScript.EmacsURL’ Info.plist
PlistBuddy -c 'Add :CFBundleURLTypes array’ Info.plist
PlistBuddy -c 'Add :CFBundleURLTypes:0:CFBundleURLName string "Emacs Helper"' Info.plist
PlistBuddy -c 'Add :CFBundleURLTypes:0:CFBundleURLSchemes array' Info.plist
PlistBuddy -c 'Add :CFBundleURLTypes:0:CFBundleURLSchemes:0 string emacs' Info.plist

First we add an identifier for the application (com.apple.AppleScript.EmacsURL). Then we add the URL-handling as an array that contains one dictionary with a name (Emacs Helper) and a scheme. The scheme is an array with a single string element (emacs). The consequence is that this application will handle the emacs URL protocol (handle URLs with the prefix emacs:). To activate this on OS X, the application should be run once after it is installed. For other details regarding compiling, modifying the Info.plist file, and installing the program, see the Makefile file.

Thursday, October 26, 2017

Run all Airmail rules

I currently use Airmail as my Mac (and iOS) mail client. With many email accounts I found that Airmail fits my needs OK. And I have created a large number of rules that is automatically performed on new emails in my inbox. However, for different reasons I would sometimes like to perform all my rules manually. As far as I know this is not possible in the Mac version of Airmail. So I had to implement it myself using AppleScript. You can find the program below. A few things you should be aware of: (i) This program do not open the Airmail application (I expect Airmail to be in use on your Mac). (ii) The program needs to be able to control your computer (see System Preferences → Security & Privacy → Privacy → Accessibility). (iii) I have tried to make it robust, but no guarantees. It could mess up.

I suggest to save this code as the application RunAllAirmailRules.app in the Applications folder of your home directory (choose File Format → Application in the save dialog in Script Editor). Enjoy.

-- An Airmail 3 application
tell application "System Events" to tell process "Airmail 3"
  set frontmost to true
  
  -- Activate (and open) "Rules" window of Airmail 3
  set rules to windows where title contains "Rules"
  if rules is {} then
    click menu item "Rules" of menu "Window" of menu bar 1
    delay 1
    set rules to windows where title contains "Rules"
  end if
  if rules is not {} then
    perform action "AXRaise" of item 1 of rules
    
    -- Traverese all rules
    set all_rules to rows of table 1 of scroll area 1 of item 1 of rules
    repeat with rule in all_rules
      
      -- Select rule and go to preview of the rule
      set the value of attribute "AXSelected" of rule to true
      set enabled_rule to the value of checkbox "Enable" of item 1 of rules as boolean
      if enabled_rule then
        click button "Preview" of item 1 of rules
        
        -- Click "Apply to ..." button
        set btn_ok to false
        repeat while not btn_ok
          try
            set btn to button "Apply to previous messages" of sheet 1 of window "Rules"
            if enabled of btn then
              set btn_ok to true
            end if
          on error msg
            delay 0.1
            set btn_ok to false
          end try
        end repeat
        click btn
        
        -- Click "Close" button
        set btn_ok to false
        repeat while not btn_ok
          try
            set btn to button "Close" of sheet 1 of window "Rules"
            if enabled of btn then
              set btn_ok to true
            end if
          on error msg
            delay 0.1
            set btn_ok to false
          end try
        end repeat
        click btn
        
      end if
    end repeat
  end if
end tell