⌘+\でPDFロックを解除する

修正したlittlebirdy
/usr/local/bin/littlebirdy

#!/bin/bash
set -e

# Copyright (c) 2012 Jake Petroules. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# In case Ghostscript resides in the current directory
PATH=.:$PATH
export PATH

# Script physical directory
cd $(dirname "$0") && SCRIPTDIR=$(pwd) && cd - > /dev/null

# Paths and filenames
CFG="$SCRIPTDIR/littlebirdy.ini"
LOG="$SCRIPTDIR/littlebirdy.log"
UNLOCKED_DIR="/tmp/littlebirdy"
OUT_DIR="./unlocked"

# If no arguments were supplied, show the usage
if [[ $# < 1 ]] ; then
	echo "Usage: $0 file1 file2 file3 ..."
	exit
fi

# Make sure Ghostscript exists... for Windows it'd be gswin32c
#if [ ! -f gs ] && [ ! $(which gs) ] ; then
#	echo "Ghostscript is missing!"
#	exit -1
#fi

# Read a list of all possible directories containing fonts, from our font config file
# Windows would need %WINDIR%/Fonts
FONTPATH=`cat "$CFG" | tr "\\n" ":"`

# Process the files
for f in "$@" ; do
    if [ ! -f "$f" ] ; then
        echo "File not found: $f"
        exit -1
    fi

	echo -n "Enter the PDF Document Open Password for $f: "
	read PASSWORD

	# A temporary file to save the unlocked PDF to
	mkdir -p $UNLOCKED_DIR
	UNLOCKED_PATH="$UNLOCKED_DIR/$(basename $0).$RANDOM.$$"

    # Run Ghostscript and append its output to the log file
    /usr/local/bin/gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sFONTPATH="$FONTPATH" \
       -sPDFPassword="$PASSWORD" -dPDFSettings=/prepress -dPassThroughJPEGImages=true \
       -sOutputFile="$UNLOCKED_PATH" "$f" >> "$LOG"

    # Move the unlocked PDF from the temporary file to the output path
	if [ -f "$UNLOCKED_PATH" ] ; then
		mkdir -p "$OUT_DIR"
	    mv "$UNLOCKED_PATH" "$OUT_DIR/$(basename $f)"
    	echo "Unlocked PDF saved to $OUT_DIR/$(basename $f)"
    fi
done

Automatorの設定
Unlock_Unprotected_PDF.workflow (Quick Action)

AppleScriptの中身

tell application "System Events"
	tell process "Preview"
		set thefile to value of attribute "AXDocument" of window 1
	end tell
end tell

-- display dialog "my variable: " & thefile
-- display dialog quoted form of replaceText(thefile, "%20", space)
set thefile to replaceText(thefile, "%20", space)
set thefile to replaceText(thefile, "File://", "")
-- set newFile to POSIX file thefile
set thefile to do shell script "/usr/local/bin/php -r 'echo urldecode(\"" & thefile & "\");'"

do shell script "/usr/local/bin/littlebirdy-nopassword " & thefile

tell application "Preview" to close window 1
do shell script "open " & thefile

to replaceText(someText, oldItem, newItem)
	(*
     replace all occurances of oldItem with newItem
          parameters -     someText [text]: the text containing the item(s) to change
                    oldItem [text, list of text]: the item to be replaced
                    newItem [text]: the item to replace with
          returns [text]:     the text with the item(s) replaced
     *)
	set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
	try
		set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
		set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
	on error errorMessage number errorNumber -- oops
		set AppleScript's text item delimiters to tempTID
		error errorMessage number errorNumber -- pass it on
	end try
	
	return someText
end replaceText

Keyboard shortcutの設定