My blog has moved to http://stuartjandrews.com!

You will be automatically redirected momentarily.

Showing posts with label Safari. Show all posts
Showing posts with label Safari. Show all posts

2011-04-19

Reopen-All-Safari-Windows-On-Login

Contents

Safari provides an option to “Reopen All Windows From Last Session” under the “History” menu, but unfortunately does not allow one to automate this upon startup.

Selecting this menu item after every system restart was becoming a little tiring, and so I wrote the following Applescript to take care of it for me. When run, this script simply tells Safari to activate this menu item.

If the script is saved as an application, it can be added to a user’s list of “Login Items”, accessible through the “System Preferences” > “Accounts”. To save the script as an application, copy-and-paste it into Applescript Editor, then use the “File” menu option “Save as ..”, and select application from the “File Format” menu.

In order to use this script, you will need to “Enable access for assistive devices”. Please read the following instructions from dougscripts.com

Setting Up System Preferences

In order for the “System Events” application to use the key code or keystroke commands, you have to enable Mac OS X’s accessibility frameworks. To do this, click on the “Universal Access” pane in “System Preferences”. At the bottom of the pane is a checkbox setting called “Enable access for assistive devices”. Click on the checkbox so the setting is enabled. Close out of System Preferences.

Here is the Applescript:

(*
Purpose: Tells Safari to "Reopen All Windows From Last Session".

Usage: Save this script as an application, and add it to your "Login Items"

In order to use this script, you will need to "Enable access for assistive 
devices".  Please read the following instructions from 
[dougscripts.com](http://dougscripts.com/itunes/itinfo/keycodes.php)

> Setting Up System Preferences
> 
> In order for the "System Events" application to use the key code or keystroke 
> commands, you have to enable Mac OS X's accessibility frameworks. To do this, 
> click on the "Universal Access" pane in "System Preferences". At the bottom of 
> the pane is a checkbox setting called "Enable access for assistive devices". 
> Click on the checkbox so the setting is enabled. Close out of System 
> Preferences.

Author: Stuart J. D. Andrews;
Email: sunny day in nyc at gmail com
Date: March 6, 2011
Environment: Script Editor 2.3; AppleScript 2.1.2; Mac OS X 10.6.7
*)

do_menu("Safari", "History", "Reopen All Windows from Last Session")

on do_menu(app_name, menu_name, menu_item)
    try
        -- bring the target application to the front
        tell application app_name
            activate
        end tell
        tell application "System Events"
            tell process app_name
                tell menu bar 1
                    tell menu bar item menu_name
                        tell menu menu_name
                            click menu item menu_item
                        end tell
                    end tell
                end tell
            end tell
        end tell
        return true
    on error error_message
        return false
    end try
end do_menu

2011-04-18

Switching-Gears-With-Applescript

Contents

Occasionally, I use Safari to view a local file. If it is a PDF, I might want to re-open it in another PDF reader (e.g. Preview or Skim). If the file is a text file with an extension recognized by my text editor, I might want to open it in my preferred text editor. This post explains how I accomplish this.

The following Applescript re-opens a local file from the frontmost window of Safari into a second application. As show below, the second application is MacVim. The script can be easily modified to open files using other applications, for example Skim. This script should be run from the Applescript menu when the document is open-and-frontmost in Safari.

The script assumes that the URL contains the path to a local file, prefixed by file://. There is no testing performed to ensure that the URL represents a local file. The prefix is removed, and the desired application is told to open the corresponding document.

Installation
Place the script in ~/Library/Scripts/Applications/Safari/. For more detailed installation instructions, visit how-to-applescript.
Keyboard Shortcut
Choose a system-wide keyboard shortcut that will “Move focus to status menus in the menu bar”. This can be set up in the “Keyboard & Mouse” System Preferences.

Ideally, this script should be extended

  1. to perform simple tests on the format of the URL, and
  2. to attempt to download a remote file before trying to open it locally

Enjoy,

  • Stu
(*
Purpose: Use this script to re-open using "MacVim" a document that is open in
"Safari".  The script detects the full path of the local file and re-opens it.
Ideally, the script would also close the file in "Safari", but this has not
been implemented.  

Usage: This script should be run from the applescript menu when the document is
open-and-frontmost in Safari.

Installation: Place the script in ~/Library/Scripts/Applications/Safari/  To
access this script, make sure to select "Show script menu in menu bar" in the
AppleScript Utility application (10.5) or in Preferences > General of the
AppleScript Editor application (10.6).

Installation for mouse and trackpad-free access:  Choose a keyboard shortcut
that will "Move focus to status menus in the menu bar".  This can be set up in
the "Keyboard & Mouse" System Preferences.

Author: Stuart J. D. Andrews;
Email: sunny day in nyc at gmail com
Date: April 15, 2011
Environment: Script Editor 2.3; AppleScript 2.1.2; Mac OS X 10.6.7

*)

property myapp : "Safari"
property debug : false
-- property debug : true

try
    set front_process to application "Finder"
    --
    tell application "Safari"
        set VVV to URL of document 1
    end tell
    
    -- remove leading "file://"
    do shell script "echo " & quoted form of VVV & "| cut -c 8-"
    set VVV to result
    errmsg(VVV, front_process)
    
    -- open file in Skim
    tell application "MacVim"
        activate
        open VVV
    end tell
    
    
on error the error_message number the error_number
    errmsg("Error: " & the error_number & ". " & the error_message, front_process)
    
end try


on errmsg(VVV, active_process)
    if debug then
        tell active_process
            activate
            display dialog "Value: " & VVV buttons {"OK"} default button 1
        end tell
    end if
end errmsg