2021年02月03日
PDFを指定したフォーマットで分割・連番出力するスクリプト
Split_PDF_into_any_format.zip
選択したPDFを同階層にソースと同じ名称のフォルダを作り、連番で出力するスクリプトです。
ネットのコードを参考にし、自分向けに?改変したものです。
スクリプトエディタから実行したり、スクリプトメニューに登録して実行したり、
Automatorでクイックアクション(サービス)を作りそこから実行することもできます。
スクリプト名:SplitPDFtoJPEG.scptd
選択したPDFを同階層にソースと同じ名称のフォルダを作り、連番で出力するスクリプトです。
ネットのコードを参考にし、自分向けに?改変したものです。
スクリプトエディタから実行したり、スクリプトメニューに登録して実行したり、
Automatorでクイックアクション(サービス)を作りそこから実行することもできます。
スクリプト名:SplitPDFtoJPEG.scptd
-- Created 2018-05-26 by Takaaki Naganoya
-- 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property |NSURL| : a reference to current application's |NSURL|
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray
tell application "Finder"
set aSel to selection as alias list
end tell
if aSel = {} then return
set sourceFiles to filterAliasListByUTI(aSel, "com.adobe.pdf") of me
--> {"/Users/me/Pictures/annotation_origin.pdf"}
set workFlow to quoted form of POSIX path of (((path to me) as text) & "Contents:Resources:workflow:PDFtoJPG.workflow:")
repeat with sourceFile in sourceFiles
tell application "Finder"
set oya to (container of (sourceFile as POSIX file as alias)) as alias
set fileName to name of (sourceFile as POSIX file as alias)
set folderName to text 1 thru -5 of fileName
tell folder oya
if not (exists folder folderName) then
make new folder at oya with properties {name:folderName}
end if
end tell
set destFolder to POSIX path of ((oya as text) & folderName)
end tell
try
set res to do shell script "automator -i " & quoted form of POSIX path of sourceFile & space & workFlow
on error msg number n
error msg & space & n
end try
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set theList to text items of res
set AppleScript's text item delimiters to oldDelimiters
set cnt to 1
repeat with convertedFile in theList
if convertedFile does not contain "alias" and convertedFile does not contain "}" then
set fullPath to POSIX path of convertedFile
do shell script "mv " & quoted form of fullPath & " " & quoted form of (destFolder & "/" & (retZeroPaddingText(cnt, 5) of me) & ".jpeg")
set cnt to cnt + 1
end if
end repeat
end repeat
--------------------
--Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j
end if
end repeat
return newList
end filterAliasListByUTI
--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath
--UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
set anArray to NSArray's arrayWithArray:aUTIList
set aPred to NSPredicate's predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
set bRes to (anArray's filteredArrayUsingPredicate:aPred) as list
return bRes
end filterUTIList
--------------------
--数値にゼロパディングしたテキストを返す
on retZeroPaddingText(aNum, aLen)
set tText to ("0000000000" & aNum as text)
set tCount to length of tText
set resText to text (tCount - aLen + 1) thru tCount of tText
return resText
end retZeroPaddingText
選択したPDFを同階層にソースと同じ名称のフォルダを作り、連番で出力するスクリプトです。
スクリプト名:SplitPDFtoPNG.scptd
スクリプト名:SplitPDFtoPNG.scptd
-- Created 2018-05-26 by Takaaki Naganoya
-- 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property |NSURL| : a reference to current application's |NSURL|
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray
tell application "Finder"
set aSel to selection as alias list
end tell
if aSel = {} then return
set sourceFiles to filterAliasListByUTI(aSel, "com.adobe.pdf") of me
--> {"/Users/me/Pictures/annotation_origin.pdf"}
set workFlow to quoted form of POSIX path of (((path to me) as text) & "Contents:Resources:workflow:PDFtoPNG.workflow:")
repeat with sourceFile in sourceFiles
tell application "Finder"
set oya to (container of (sourceFile as POSIX file as alias)) as alias
set fileName to name of (sourceFile as POSIX file as alias)
set folderName to text 1 thru -5 of fileName
tell folder oya
if not (exists folder folderName) then
make new folder at oya with properties {name:folderName}
end if
end tell
set destFolder to POSIX path of ((oya as text) & folderName)
end tell
try
set res to do shell script "automator -i " & quoted form of POSIX path of sourceFile & space & workFlow
on error msg number n
error msg & space & n
end try
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set theList to text items of res
set AppleScript's text item delimiters to oldDelimiters
set cnt to 1
repeat with convertedFile in theList
if convertedFile does not contain "alias" and convertedFile does not contain "}" then
set fullPath to POSIX path of convertedFile
--do shell script "mv " & quoted form of fullPath & " " & quoted form of destFolder
do shell script "mv " & quoted form of fullPath & " " & quoted form of (destFolder & "/" & (retZeroPaddingText(cnt, 5) of me) & ".png")
set cnt to cnt + 1
end if
end repeat
end repeat
--------------------
--Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j
end if
end repeat
return newList
end filterAliasListByUTI
--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath
--UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
set anArray to NSArray's arrayWithArray:aUTIList
set aPred to NSPredicate's predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
set bRes to (anArray's filteredArrayUsingPredicate:aPred) as list
return bRes
end filterUTIList
--------------------
--数値にゼロパディングしたテキストを返す
on retZeroPaddingText(aNum, aLen)
set tText to ("0000000000" & aNum as text)
set tCount to length of tText
set resText to text (tCount - aLen + 1) thru tCount of tText
return resText
end retZeroPaddingText
動画は、上記スクリプトをクイックアクション(サービス)に組み込んで実行した時の動作です。
PDFを分割して連番のイメージファイルに変換.zip
以下、バージョン2です。
スクリプト名:Split PDF to JPEG v2.scptd
--Original By Shane Stanley 2020/1/28
--Modified by Takaaki Naganoya 2020/6/4
--Error reported by hiro 2020/6/10
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "Automator"
use scripting additions
property |NSURL| : a reference to current application's |NSURL|
property AMWorkflow : a reference to current application's AMWorkflow
property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray
set thePath to POSIX path of (((path to me) as string) & "Contents:Resources:workflow:Split PDF to Image file.workflow:")
tell application "Finder" to set aSel to the selection as alias list
if aSel = {} then return
set filRes1 to filterAliasListByUTI(aSel, "com.adobe.pdf") of me
repeat with i in filRes1
set aFile to contents of i
set {theResult, theError} to runWorkflow(thePath, aFile) of me
end repeat
on runWorkflow(thePath, theInput)
set theURL to |NSURL|'s fileURLWithPath:thePath
set {theResult, theError} to AMWorkflow's runWorkflowAtURL:(theURL) withInput:theInput |error|:(reference)
return {theResult, theError}
end runWorkflow
----------
--Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j
end if
end repeat
return newList
end filterAliasListByUTI
--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath
--UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
set anArray to NSArray's arrayWithArray:aUTIList
set aPred to NSPredicate's predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
set bRes to (anArray's filteredArrayUsingPredicate:aPred) as list
return bRes
end filterUTIList
----------
スクリプト名:Split PDF to PNG v2.scptd
--Original By Shane Stanley 2020/1/28
--Modified by Takaaki Naganoya 2020/6/4
--Error reported by hiro 2020/6/10
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "Automator"
use scripting additions
property |NSURL| : a reference to current application's |NSURL|
property AMWorkflow : a reference to current application's AMWorkflow
property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray
set thePath to POSIX path of (((path to me) as string) & "Contents:Resources:workflow:Split PDF to Image file.workflow:")
tell application "Finder" to set aSel to the selection as alias list
if aSel = {} then return
set filRes1 to filterAliasListByUTI(aSel, "com.adobe.pdf") of me
repeat with i in filRes1
set aFile to contents of i
set {theResult, theError} to runWorkflow(thePath, aFile) of me
end repeat
on runWorkflow(thePath, theInput)
set theURL to |NSURL|'s fileURLWithPath:thePath
set {theResult, theError} to AMWorkflow's runWorkflowAtURL:(theURL) withInput:theInput |error|:(reference)
return {theResult, theError}
end runWorkflow
----------
--Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j
end if
end repeat
return newList
end filterAliasListByUTI
--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)
if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath
--UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
set anArray to NSArray's arrayWithArray:aUTIList
set aPred to NSPredicate's predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
set bRes to (anArray's filteredArrayUsingPredicate:aPred) as list
return bRes
end filterUTIList
----------
更新履歴
- 2021年01月13日新規掲載ラベル
- 2021年02月03日バージョン2を掲載ラベル
- 20xx年xx月xx日更新情報が入りますラベル
- 20xx年xx月xx日更新情報が入りますラベル
- 20xx年xx月xx日更新情報が入りますラベル