[xpdf]コマンドライン・ヘルパ8種
添付操作のデタッチ pdfdetach
フォントツール pdffonts
基本情報取得 pdfinfo
EPS書き出し pdftops
PS書き出し pdftops
PNG書き出し pdftopng
HTML書き出し pdftohtml
テキスト書き出し pdftotext
pdftohtml HTMLへの書き出しツールはサブセットされたフォントデータを取り出すこともできる優れもの
フォントツール pdffonts
基本情報取得 pdfinfo
EPS書き出し pdftops
PS書き出し pdftops
PNG書き出し pdftopng
HTML書き出し pdftohtml
テキスト書き出し pdftotext
pdftohtml HTMLへの書き出しツールはサブセットされたフォントデータを取り出すこともできる優れもの
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# xpdfコマンドラインツールをインストールします
# インストール不要な方は実行しないでください
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
###モジュールチェックが不要な場合は削除
set strBinPath to ("~/bin/xpdf/binARM/pdftohtml") as text
set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
set ocidBinPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:false)
set strBinPath to (ocidBinPathURL's |path|) as text
set ocidArmDirPathURL to ocidBinPathURL's URLByDeletingLastPathComponent()
set ocidContainerDirPathURL to ocidArmDirPathURL's URLByDeletingLastPathComponent()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidContainerDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
set strContainerDirPath to (ocidContainerDirPathURL's |path|) as text
##
set boolFileExists to (appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:false)
if boolFileExists is true then
log "pdffontsインストール済み"
else if boolFileExists is false then
log "インストールします"
set strURL to ("https://dl.xpdfreader.com/xpdf-tools-mac-4.05.tar.gz") as text
set ocidURLstr to (refMe's NSString's stringWithString:(strURL))
set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstr)
set listResponse to refMe's NSData's dataWithContentsOfURL:(ocidURL) options:(refMe's NSDataReadingMappedIfSafe) |error|:(reference)
if (item 2 of listResponse) is not (missing value) then
log "ダウンロードに失敗しました"
return (item 2 of listResponse)'s localizedDescription() as text
else
set ocidGzData to (item 1 of listResponse)
end if
#
set ocidTempDirPathURL to appFileManager's temporaryDirectory()
set strTempDirPath to (ocidTempDirPathURL's |path|) as text
set ocidGzFilePathURL to ocidTempDirPathURL's URLByAppendingPathComponent:"xpdf.tar.gz" isDirectory:false
set strGzFilePathURL to (ocidGzFilePathURL's |path|) as text
set listDone to ocidGzData's writeToURL:(ocidGzFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
if (item 1 of listDone) is false then
log "ダウンロードに失敗しました"
return (item 2 of listDone)'s localizedDescription() as text
else
try
set strCommandText to ("/usr/bin/bsdtar -xzvf \"" & strGzFilePathURL & "\" -C \"" & strTempDirPath & "\"") as text
do shell script strCommandText
on error
return "ファイルの解凍に失敗しました"
end try
try
set strCommandText to ("/usr/bin/ditto \"" & strTempDirPath & "/xpdf-tools-mac-4.05\" \"" & strContainerDirPath & "\"") as text
do shell script strCommandText
on error
return "ファイルのコピーに失敗しました"
end try
end if
end if
##################################
#### ファイルを選択
##################################
####ダイアログで
tell current application
set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
tell application "Finder"
activate
end tell
else
tell current application
activate
end tell
end if
##PDF
set aliasFilePath to (choose file with prompt "PDFファイルを選んでください" default location (path to desktop folder from user domain) of type {"com.adobe.pdf"} with invisibles and showing package contents without multiple selections allowed) as alias
##################################
### パス関連
##################################
set strFilePath to POSIX path of aliasFilePath as text
set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false)
set ocidFileName to ocidFilePathURL's lastPathComponent()
set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() as text
##################################
#### 保存先
##################################
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
set strMes to "保存先フォルダを選んでください" as text
set strPrompt to "保存先フォルダを選択してください" as text
try
set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasContainerDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
##
set strSaveDirPath to POSIX path of aliasSaveDirPath as text
set ocidSaveDirPathStr to (refMe's NSString's stringWithString:(strSaveDirPath))
set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath
set ocidSaveURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
set ocidSaveDirPathURL to ocidSaveURL's URLByAppendingPathComponent:(ocidBaseFileName) isDirectory:(true)
set strSaveDirPath to (ocidSaveDirPathURL's |path|) as text
####コマンド実行
set strCommandText to ("\"" & strBinPath & "\" -overwrite \"" & strFilePath & "\" \"" & strSaveDirPath & "\"")
log strCommandText
set ocidCommandText to refMe's NSString's stringWithString:strCommandText
set ocidTermTask to refMe's NSTask's alloc()'s init()
ocidTermTask's setLaunchPath:"/bin/zsh"
ocidTermTask's setArguments:({"-c", ocidCommandText})
set ocidPipe to refMe's NSPipe's pipe
ocidTermTask's setStandardOutput:(ocidPipe)
ocidTermTask's |launch|()
ocidTermTask's waitUntilExit()
if (ocidTermTask's terminationStatus as text) is "0" then
set ocidHandle to ocidPipe's fileHandleForReading()
set ocidResponse to ocidHandle's readDataToEndOfFile()
set ocidResponseStr to refMe's NSString's alloc()'s initWithData:(ocidResponse) encoding:(refMe's NSUTF8StringEncoding)
else
log ocidTermTask's terminationStatus as text
log ocidTermTask's terminationReason as text
return "コマンド実行でエラーしました"
end if
set aliasOpenDirPath to (ocidSaveDirPathURL's absoluteURL()) as alias
tell application "Finder"
open location aliasOpenDirPath
end tell
| 固定リンク
「AppleScript PDF」カテゴリの記事
- Quartz PDFContext AppendModeについてのメモ(2024.10.17)
- [cpdf]見開き2in1ページを左右に分割(2024.09.09)
- [PDF]見開き2in1ページを左右に分割 まとめ(2024.09.10)
- PDF2in1になっている見開きPDFを左右にページを分割する(2024.09.05)
- PDFで使用されているフォントをページ毎に調べてテキスト出力する(2024.08.30)