AppleScript PDFDisplayBox

[PDFDocumentWriteOption] writeToFile:withOptionsで保存

[PDFDisplayBox]新規ブランクPDFを作成する(塗りたし有り)
https://quicktimer.cocolog-nifty.com/icefloe/2023/05/post-7f8a01.html
スクリプトに保存オプションつけたパターン
PDFDocumentOptimizeImagesForScreenOption
PDFPageImageInitializationOptionCompressionQuality

指定しています
Pdfmediabox00002002
パスワード等については
暗号化とパスワード指定を見てください
https://quicktimer.cocolog-nifty.com/icefloe/cat76054342/index.html

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "Quartz"
use framework "PDFKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

###########################
###サイズ指定
###########################
##仕上がり寸法CropBox ミリ指定
set numWmm to 420 as integer
set numHmm to 297 as integer
###PT換算しておく
set numWpt to (numWmm / 0.3527777778) as number
set numHpt to (numHmm / 0.3527777778) as number

##塗りたし ミリ指定
set numBmm to 3
##通常はこちら
set numBpt to (numBmm / 0.3527777778) as number


###########################
###ダイアログを前面に出す
###########################
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
###########################
###ダイアログ
###########################
set strDefaultName to "ファイル名.pdf" as text
set strExtension to "pdf"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strPromptText to "名前を決めてください" as text
set strMesText to "名前を決めてください" as text
set aliasFilePath to (choose file name strMesText default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strFilePath to (POSIX path of aliasFilePath) as text
###パスをリストにして
set AppleScript's text item delimiters to "/"
set listPathComponent to every text item of strFilePath
set AppleScript's text item delimiters to ""
####最後のアイテムがファイル名
set strFileName to (last item of listPathComponent) as text
###拡張子取っちゃった場合対応用
if strFileName does not contain strExtension then
  set strFileName to (strFileName & "." & strExtension) as text
end if
###保存先ファイルURL
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath)

###########################
###PDFKIT本処理
###########################

####ドキュメントの初期化
set ocdiActivDoc to refMe's PDFDocument's alloc()'s init()
####メタデータ
set ocidAttributesDictionaryM to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###タイトルはファイル名
ocidAttributesDictionaryM's setValue:(strFileName) forKey:"Title"
###作者はログインユーザー
set strUserName to (refMe's NSFullUserName()) as text
ocidAttributesDictionaryM's setValue:(strUserName) forKey:"Author"
set ocdiActivDoc's documentAttributes to ocidAttributesDictionaryM

####ページの初期化
set ocidPDFPage to refMe's PDFPage's alloc()'s init()

###A3横 Media と Bleed (塗りたしは左右上下にあるので倍になる)
set ocidMediaRect to refMe's CGRectMake(0, 0, (numWpt + (numBpt * 2)), (numHpt + (numBpt * 2)))
ocidPDFPage's setBounds:(ocidMediaRect) forBox:(refMe's kPDFDisplayBoxMediaBox)
ocidPDFPage's setBounds:(ocidMediaRect) forBox:(refMe's kPDFDisplayBoxBleedBox)

###A3横 Crop と Trim の Rect(左下基準の位置からの位置なので塗りたし分だけ加算)
set ocidCropRect to refMe's CGRectMake(numBpt, numBpt, numWpt, numHpt)
ocidPDFPage's setBounds:(ocidCropRect) forBox:(refMe's kPDFDisplayBoxCropBox)
ocidPDFPage's setBounds:(ocidCropRect) forBox:(refMe's kPDFDisplayBoxTrimBox)
###ARTBOXは本来指定しないが空のPDFなのでCROPと同値にする
ocidPDFPage's setBounds:(ocidCropRect) forBox:(refMe's kPDFDisplayBoxArtBox)

###PDFドキュメントにPDFページを挿入
ocdiActivDoc's insertPage:ocidPDFPage atIndex:0
ocidPDFPage's release()

##保存オプション
set ocidPDFoption to {PDFDocumentOptimizeImagesForScreenOption:true, PDFPageImageInitializationOptionCompressionQuality:1}
####ファイルに書き出す writeToFile:withOptions:で保存
ocdiActivDoc's writeToURL:(ocidFilePathURL) withOptions:ocidPDFoption
ocdiActivDoc's release()

return


|

[PDFDisplayBox]新規ブランクPDFを作成する(塗りたし有り)

このパターン
表示サイズの外に3mm塗りたし
メディアサイズと塗りたしサイズが縦横それぞれ6mm大きいパターン
Pdfmediabox00002002

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "Quartz"
use framework "PDFKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

###########################
###サイズ指定
###########################
##仕上がり寸法CropBox ミリ指定
set numWmm to 420 as integer
set numHmm to 297 as integer
###PT換算しておく
set numWpt to (numWmm / 0.3527777778) as number
set numHpt to (numHmm / 0.3527777778) as number

##塗りたし ミリ指定
set numBmm to 3
##通常はこちら
set numBpt to (numBmm / 0.3527777778) as number


###########################
###ダイアログを前面に出す
###########################
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
###########################
###ダイアログ
###########################
set strDefaultName to "ファイル名.pdf" as text
set strExtension to "pdf"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strPromptText to "名前を決めてください" as text
set strMesText to "名前を決めてください" as text
set aliasFilePath to (choose file name strMesText default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
set strFilePath to (POSIX path of aliasFilePath) as text
###パスをリストにして
set AppleScript's text item delimiters to "/"
set listPathComponent to every text item of strFilePath
set AppleScript's text item delimiters to ""
####最後のアイテムがファイル名
set strFileName to (last item of listPathComponent) as text
###拡張子取っちゃった場合対応用
if strFileName does not contain strExtension then
  set strFileName to (strFileName & "." & strExtension) as text
end if
###保存先ファイルURL
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath)

###########################
###PDFKIT本処理
###########################

####ドキュメントの初期化
set ocdiActivDoc to refMe's PDFDocument's alloc()'s init()
####メタデータ
set ocidAttributesDictionaryM to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###タイトルはファイル名
ocidAttributesDictionaryM's setValue:(strFileName) forKey:"Title"
###作者はログインユーザー
set strUserName to (refMe's NSFullUserName()) as text
ocidAttributesDictionaryM's setValue:(strUserName) forKey:"Author"
set ocdiActivDoc's documentAttributes to ocidAttributesDictionaryM

####ページの初期化
set ocidPDFPage to refMe's PDFPage's alloc()'s init()

###A3横 Media と Bleed (塗りたしは左右上下にあるので倍になる)
set ocidMediaRect to refMe's CGRectMake(0, 0, (numWpt + (numBpt * 2)), (numHpt + (numBpt * 2)))
ocidPDFPage's setBounds:(ocidMediaRect) forBox:(refMe's kPDFDisplayBoxMediaBox)
ocidPDFPage's setBounds:(ocidMediaRect) forBox:(refMe's kPDFDisplayBoxBleedBox)

###A3横 Crop と Trim の Rect(左下基準の位置からの位置なので塗りたし分だけ加算)
set ocidCropRect to refMe's CGRectMake(numBpt, numBpt, numWpt, numHpt)
ocidPDFPage's setBounds:(ocidCropRect) forBox:(refMe's kPDFDisplayBoxCropBox)
ocidPDFPage's setBounds:(ocidCropRect) forBox:(refMe's kPDFDisplayBoxTrimBox)
###ARTBOXは本来指定しないが空のPDFなのでCROPと同値にする
ocidPDFPage's setBounds:(ocidCropRect) forBox:(refMe's kPDFDisplayBoxArtBox)

###PDFドキュメントにPDFページを挿入
ocdiActivDoc's insertPage:ocidPDFPage atIndex:0
ocidPDFPage's release()

####ファイルに書き出す
ocdiActivDoc's writeToURL:(ocidFilePathURL)
ocdiActivDoc's release()

return


|

[PDFKit]表示サイズ(CropBox)を用紙サイズ(MediaBox)と同じに変更する

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions

property refMe : a reference to current application

##################################
#### 文書を開いているか?
##################################
tell application id "com.adobe.Reader"
  activate
  tell active doc
    set numAllPage to do script ("this.numPages;")
    try
      if numAllPage is "undefined" then
        error number -1708
      end if
    on error
      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
      display alert "エラー:文書が選択されていません" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 10
      return "エラー:文書が選択されていません"
    end try
  end tell
end tell
#######################
#####Acraobatでのページ指定
#######################
tell application id "com.adobe.Reader"
  activate
  ##ファイルパス
  tell active doc
    set aliasFilePath to file alias
  end tell
  ##開いているファイルのページ数
  tell active doc
    set numCntAllPage to (count every page) as integer
  end tell
  ##表示中のページ番号
  tell active doc
    tell front PDF Window
      set numNowPage to page number as integer
      set numNowPageJs to numNowPage - 1 as integer
    end tell
  end tell
  ##表示中のページの『今』の回転
  tell active doc
    tell front PDF Window
      tell page numNowPage
        set numNowRotation to rotation as integer
      end tell
    end tell
  end tell
end tell

##################################
####一旦閉じる
##################################
tell application id "com.adobe.Reader"
  activate
  set objAvtivDoc to active doc
  tell objAvtivDoc
    set boolMode to modified
    ###変更箇所があるなら保存する
    if boolMode is true then
      save
    end if
  end tell
  close objAvtivDoc
end tell

#######################
#####本処理
#######################
####パス
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

####PDFファイルを格納
set ocidPDFDocument to refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL
###ページ数数えて
set numAllPage to ocidPDFDocument's pageCount() as integer
set numNowPageJs to 0 as integer

repeat numAllPage times
  ####PDFの対象ページを格納
  set ocidPdfPage to ocidPDFDocument's pageAtIndex:numNowPageJs
  
  ###ページのメディアボックス(用紙サイズ)を取得
  set ocidMedeiaBox to ocidPdfPage's boundsForBox:(refMe's kPDFDisplayBoxMediaBox)
  ###用紙サイズ=表示サイズにする
  ocidPdfPage's setBounds:ocidMedeiaBox forBox:(refMe's kPDFDisplayBoxCropBox)
  (*
ocidPdfPage's setBounds:ocidMedeiaBox forBox:(refMe's kPDFDisplayBoxMediaBox)
ocidPdfPage's setBounds:ocidMedeiaBox forBox:(refMe's kPDFDisplayBoxCropBox)
ocidPdfPage's setBounds:ocidMedeiaBox forBox:(refMe's kPDFDisplayBoxBleedBox)
ocidPdfPage's setBounds:ocidMedeiaBox forBox:(refMe's kPDFDisplayBoxTrimBox)
ocidPdfPage's setBounds:ocidMedeiaBox forBox:(refMe's kPDFDisplayBoxArtBox)
*)
  
  set numNowPageJs to numNowPageJs + 1 as integer
end repeat

#################################
#####保存する 
#################################
ocidPDFDocument's writeToURL:(ocidFilePathURL)
delay 1

#################################
#####アクロバットで開く
#################################
(*
open option
https://quicktimer.cocolog-nifty.com/icefloe/2023/03/post-bcc393.html
*)
set strOptionText to "page=" & numNowPage & "&zoom=top&view=Fit&pagemode=thumbs" as text
tell application id "com.adobe.Reader"
  activate
  tell front PDF Window
    open aliasFilePath options strOptionText
  end tell
  tell active doc
    tell front PDF Window
      set page number to numNowPage
    end tell
  end tell
end tell

|

[PDFKit]用紙サイズ(MediaBox)変更(その2)

用紙サイズ(MediaBox)をA3に変更します。
その際に
立落としサイズ(仕上がりサイズ)TrimBoxに3mm足したところに
塗り足しサイズ(BleedBox)を設定します。



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
use framework "AppKit"
use framework "Quartz"
use framework "QuartzCore"

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
property objNSArray : a reference to NSArray

######ログ表示
doLogView()


tell application "System Events"
set strAppTitile to title of (front process whose frontmost is true)
end tell


if strAppTitile is "プレビュー" then
tell application "Preview"
tell document 1
set strFilePath to path as text
close
end tell
end tell
else
set aliasFile to (choose file with prompt "ファイルを選んでください" 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
-->alias
set strFilePath to POSIX path of aliasFile
end if



####ドキュメントのパスをNSString
set ocidFilePath to objNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidNSUrlPath to objNSURL's fileURLWithPath:ocidFilePath
####PDFファイルを格納
set ocidPDFDocument to objMe's PDFDocument's alloc()'s initWithURL:ocidNSUrlPath
################################################
#######
################################################
####ページ数を数える
set numOrgPdfPageCnt to ocidPDFDocument's pageCount()
####最後のページから逆順に処理する(0ベースページ数なので1引く)
set numCntPageNo to 0 as number
####ページ数だけ繰り返し
repeat numOrgPdfPageCnt times
#####ページをocidPdfPageObjに格納
set ocidPdfPageObj to (ocidPDFDocument's pageAtIndex:numCntPageNo)
--> ocidPdfPageObjPDF書類の各ページが順番に入ります

####現在の回転を確認
set strPageRotation to ocidPdfPageObj's |rotation|()
log "▼現在の回転:" & strPageRotation
####まずはページの回転に0を入れて各ページを『天地向』にします
####
ocidPdfPageObj's setRotation:0
####ocidPdfPageObj's setRotation:90
####ocidPdfPageObj's setRotation:180
####ocidPdfPageObj's setRotation:270
(*
誤解している人が多いが
見た目が回転している『だけ』なので
天地も一緒に回転してしまいますので
180回転させると天地向が逆(下が天)になります
*)
set strPageRotation to ocidPdfPageObj's |rotation|()
log "▼設定後の回転:" & strPageRotation
#####ocidPdfPageObjの各種サイズを取得
###メディアサイズ=一般的には用紙サイズ
set ocidListPDFMediaBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxMediaBox))

#### CGRect
(*
https://developer.apple.com/documentation/corefoundation/cgrect
{{origin-x,origin-y},{size-w,size-h}}
このようにorigin=起点情報のリスト
幅と高さのsize情報のリストの2要素もったリスト
-->2つのリストを内包したリスト形式になります
*)
#############################################
### メディアサイズ=用紙サイズの起点を取得(基本0,0なはず)
set numOriginX to (item 1 of (item 1 of ocidListPDFMediaBox) as list) as number
set numOriginY to (item 2 of (item 1 of ocidListPDFMediaBox) as list) as number
### メディアサイズ=用紙サイズの縦横を取得 ptサイズ
set numMediaWPt to (item 1 of (item 2 of ocidListPDFMediaBox) as list) as number
set numMediaHPt to (item 2 of (item 2 of ocidListPDFMediaBox) as list) as number
#####################
### not イコールは /= と入力すると  になるよ
if numOriginX0 then
return "メディア原点Xが0以外"
end if
if numOriginY0 then
return "メディア原点Yが0以外"
end if
#############################################
(*
1 mm = 2.8346456693 point
1 point0.3527777778 mm
*)

## A3サイズ297×420mm to Pt
set strA3wPt to (297 * 2.8346456693) as number
set strA3hPt to (420 * 2.8346456693) as number
#############################################
if numMediaWPt > numMediaHPt then
log "横形ランドスケープ形PDF"
## A3サイズ297×420mm
set listMediaBoxDimentionA3H to {{numOriginX, numOriginY}, {strA3hPt, strA3wPt}} as list
###今の所ここでメディアサイズを決めている
--> listMediaBoxDimentionA3H = A3の横型
set ocidPdfDimention to objNSArray's arrayWithArray:listMediaBoxDimentionA3H
###############
else if numMediaWPt < numMediaHPt then
log "縦形ポートレイト形PDF"
## A3サイズ297×420mm
set listMediaBoxDimentionA3P to {{numOriginX, numOriginY}, {strA3wPt, strA3hPt}} as list
###今の所ここでメディアサイズを決めている
--> listMediaBoxDimentionA3H = A3の横型
set ocidPdfDimention to objNSArray's arrayWithArray:listMediaBoxDimentionA3P
else
log "正方形PDF"
end if
#############################################
###↑ここで決めたメディアサイズのサイズの値
set numPdfDimentionWPt to (item 1 of (item 2 of ocidPdfDimention) as list) as number
set numPdfDimentionHPt to (item 2 of (item 2 of ocidPdfDimention) as list) as number
###A3A4の差分を計算
set numDiffwPt to (numPdfDimentionWPt - numMediaWPt) / 2 as number
set numDiffhPt to (numPdfDimentionHPt - numMediaHPt) / 2 as number
###
set listDiffMediaBoxDimention to {{-numDiffwPt, -numDiffhPt}, {numPdfDimentionWPt, numPdfDimentionHPt}} as list
set ocidPdfDimentionDiff to objNSArray's arrayWithArray:listDiffMediaBoxDimention
#############################################
###MediaBox
###メディアサイズ=一般的には用紙サイズ
ocidPdfPageObj's setBounds:ocidPdfDimentionDiff forBox:(objMe's kPDFDisplayBoxMediaBox)
##########################################
### Crop
###トリミングサイズ=表示サイズ=可視領域
-->あくまでも『画面表示』されるエリアの指定で他の値とは別
-->通常印刷されるエリアはこれ=等落としサイズが望ましい
ocidPdfPageObj's setBounds:ocidPdfDimentionDiff forBox:(objMe's kPDFDisplayBoxCropBox)
#######################################
####ここでMediaBox用紙サイズは変更済み
#######################################
###変更後のMediaBox用紙サイズを再取得
set ocidListPDFMediaBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxMediaBox))
set numPdfMediaBoxDimentionWPt to (item 1 of (item 2 of ocidListPDFMediaBox) as list) as number
set numPdfMediaBoxDimentionHPt to (item 2 of (item 2 of ocidListPDFMediaBox) as list) as number
##########################################
###TrimBox
###立落としサイズ(仕上がりサイズ)
set ocidListPDFTrimBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxTrimBox))
#############################################
### メディアサイズ=用紙サイズの起点を取得(基本0,0なはず)
set numOriginTrimBoxX to (item 1 of (item 1 of ocidListPDFTrimBox) as list) as number
set numOriginTrimBoxY to (item 2 of (item 1 of ocidListPDFTrimBox) as list) as number
### メディアサイズ=用紙サイズの縦横を取得 ptサイズ
set numTrimBoxWPt to (item 1 of (item 2 of ocidListPDFTrimBox) as list) as number
set numTrimBoxHPt to (item 2 of (item 2 of ocidListPDFTrimBox) as list) as number
##########################################
###BleedBox
###塗り足しサイズ(通常3mm程度)
###塗り足し寸法
set numBleedmm to 3 as number
##Pt換算
set numBleedmmPt to (numBleedmm * 2.8346456693) as number
###メディアサイズー塗り足しサイズ2(余白は左右あるからね)=片方の余白
set strOriginBleedBoxX to (numOriginTrimBoxX - numBleedmmPt) as number
set strOriginBleedBoxY to (numOriginTrimBoxY - numBleedmmPt) as number
###Trimサイズ=仕上がり寸法に塗り足しを足す
set numBleedwPt to numTrimBoxWPt + (numBleedmmPt * 2) as number
set numBleedhPt to numTrimBoxHPt + (numBleedmmPt * 2) as number
set listDiffBleedBoxDimention to {{strOriginBleedBoxX, strOriginBleedBoxY}, {numBleedwPt, numBleedhPt}} as list
set ocidPdfBleedBoxDimention to objNSArray's arrayWithArray:listDiffBleedBoxDimention
###塗り足しサイズを設定
ocidPdfPageObj's setBounds:ocidPdfBleedBoxDimention forBox:(objMe's kPDFDisplayBoxBleedBox)


#####ページカウントアップ
set numCntPageNo to numCntPageNo + 1 as number
end repeat

####保存
ocidPDFDocument's writeToURL:(ocidNSUrlPath)



#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

[PDFKit]用紙サイズ(MediaBox)変更(その1)

サンプルです

ダウンロード - 257x364.zip


_20220724_11_29_082

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
use framework "AppKit"
use framework "Quartz"
use framework "QuartzCore"

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
property objNSArray : a reference to NSArray

######ログ表示
doLogView()


tell application "System Events"
set strAppTitile to title of (front process whose frontmost is true)
end tell


if strAppTitile is "プレビュー" then
tell application "Preview"
tell document 1
set strFilePath to path as text
close
end tell
end tell
else
set aliasFile to (choose file with prompt "ファイルを選んでください" 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
-->alias
set strFilePath to POSIX path of aliasFile
end if



####ドキュメントのパスをNSString
set ocidFilePath to objNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidNSUrlPath to objNSURL's fileURLWithPath:ocidFilePath
####PDFファイルを格納
set ocidPDFDocument to objMe's PDFDocument's alloc()'s initWithURL:ocidNSUrlPath
################################################
####### 天地向
################################################
####ページ数を数える
set numOrgPdfPageCnt to ocidPDFDocument's pageCount()
####最後のページから逆順に処理する(0ベースページ数なので1引く)
set numCntPageNo to 0 as number
####ページ数だけ繰り返し
repeat numOrgPdfPageCnt times
#####ページをocidPdfPageObjに格納
set ocidPdfPageObj to (ocidPDFDocument's pageAtIndex:numCntPageNo)
--> ocidPdfPageObjPDF書類の各ページが順番に入ります

####現在の回転を確認
set strPageRotation to ocidPdfPageObj's |rotation|()
log "▼現在の回転:" & strPageRotation
####まずはページの回転に0を入れて各ページを『天地向』にします
#### ocidPdfPageObj's setRotation:0
#### ocidPdfPageObj's setRotation:90
#### ocidPdfPageObj's setRotation:180
#### ocidPdfPageObj's setRotation:270
(*
誤解している人が多いが
見た目が回転している『だけ』なので
天地も一緒に回転してしまいますので
180回転させると天地向が逆(下が天)になります
*)
set strPageRotation to ocidPdfPageObj's |rotation|()
log "▼設定後の回転:" & strPageRotation
#####ocidPdfPageObjの各種サイズを取得
###メディアサイズ=一般的には用紙サイズ
set ocidListPDFMediaBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxMediaBox))
###塗り足しサイズ(通常3mm程度)
set ocidListPDFBleedBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxBleedBox))
###等落としサイズ(仕上がりサイズ)
set ocidListPDFTrimBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxTrimBox))
###印刷オブジェクトのサイズ(ページコンテンツの境界)
set ocidListPDFArtBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxArtBox))
###Crop=トリミングサイズ=表示サイズ=可視領域
-->あくまでも『画面表示』されるエリアの指定で他の値とは別
set ocidListPDFCropBox to (ocidPdfPageObj's boundsForBox:(objMe's kPDFDisplayBoxCropBox))

#####各種サイズのログ CGRect
log ocidListPDFMediaBox as list
log ocidListPDFBleedBox as list
log ocidListPDFTrimBox as list
log ocidListPDFArtBox as list
log ocidListPDFCropBox as list
(*
1 mm = 2.8346456693 point
1 point0.3527777778 mm
*)

#### CGRect
(*
https://developer.apple.com/documentation/corefoundation/cgrect
{{origin-x,origin-y},{size-w,size-h}}
このようにorigin=起点情報のリスト
幅と高さのsize情報のリストの2要素もったリスト
-->2つのリストを内包したリスト形式になります
*)
##########################################
### MediaBox
## A3 297×420
set strA3w to 297 as number
set strA3h to 420 as number
##Pt換算
set strA3wPt to (strA3w * 2.83465) as number
set strA3hPt to (strA3h * 2.83465) as number
### メディアサイズの縦横を取得 ptサイズ
set numMediaWPt to (item 1 of (item 2 of ocidListPDFMediaBox) as list) as number
set numMediaHPt to (item 2 of (item 2 of ocidListPDFMediaBox) as list) as number
###A3A4の差分を計算
set numA3A4wPt to (strA3wPt - numMediaWPt) / 2 as number
set numA3A4hPt to (strA3hPt - numMediaHPt) / 2 as number
###メディアサイズの原点
set strOriginX to -numA3A4wPt as number
set strOriginY to -numA3A4hPt as number
###値を整形 A4そのままの時はこちら
### set listPdfMediaBoxDimention to {{strOriginX, strOriginY}, {strA4wPt, strA4hPt}} as list
###値を整形 A3に変更する場合はこちら
set listPdfMediaBoxDimention to {{strOriginX, strOriginY}, {strA3wPt, strA3hPt}} as list
###NSArray形式に変更
set ocidPdfMediaBoxDimention to objNSArray's arrayWithArray:listPdfMediaBoxDimention
###メディアサイズ=一般的には用紙サイズ
ocidPdfPageObj's setBounds:ocidPdfMediaBoxDimention forBox:(objMe's kPDFDisplayBoxMediaBox)

##########################################
##### TrimBox=仕上がりサイズがA4の場合
set strA4w to 210 as number
set strA4h to 297 as number
##Pt換算
set strA4wPt to (strA4w * 2.83465) as number
set strA4hPt to (strA4h * 2.83465) as number

###メディアサイズの縦横を取得
set strMediaWPt to (item 1 of (item 2 of ocidListPDFMediaBox) as list) as number
set strMediaHPt to (item 2 of (item 2 of ocidListPDFMediaBox) as list) as number

###メディアサイズー仕上がりサイズ2(余白は左右あるからね)=片方の余白
set strOriginX to ((strMediaWPt - strA4wPt) / 2) as number
set strOriginY to ((strMediaHPt - strA4hPt) / 2) as number
###値を整形
set listPdfTrimBoxDimention to {{strOriginX, strOriginY}, {strA4wPt, strA4hPt}} as list
###NSArray形式に変更
set ocidPdfTrimBoxDimention to objNSArray's arrayWithArray:listPdfTrimBoxDimention
###等落としサイズ(仕上がりサイズ)
ocidPdfPageObj's setBounds:ocidPdfTrimBoxDimention forBox:(objMe's kPDFDisplayBoxTrimBox)

##########################################
###塗り足し寸法
set numBleedmm to 3 as number
##Pt換算
set strA4Bleedw to strA4w + (numBleedmm * 2) as number
set strA4Bleedh to strA4h + (numBleedmm * 2) as number
##Pt換算
set strA4wBleedPt to (strA4Bleedw * 2.83465) as number
set strA4hBleedPt to (strA4Bleedh * 2.83465) as number
###メディアサイズー仕上がりサイズ2(余白は左右あるからね)=片方の余白
set strOriginBleedX to ((strMediaWPt - strA4wBleedPt) / 2) as number
set strOriginBleedY to ((strMediaHPt - strA4hBleedPt) / 2) as number
###値を整形
set listPdfBleedBoxDimention to {{strOriginBleedX, strOriginBleedY}, {strA4wBleedPt, strA4hBleedPt}} as list
###NSArray形式に変更
set ocidPdfBleedBoxDimention to objNSArray's arrayWithArray:listPdfBleedBoxDimention
###塗り足しサイズ(通常3mm程度)
ocidPdfPageObj's setBounds:ocidPdfBleedBoxDimention forBox:(objMe's kPDFDisplayBoxBleedBox)


##########################################
### ArtBox
###印刷オブジェクトのサイズ(ページコンテンツの境界=設定値を変更してはダメ)
ocidPdfPageObj's setBounds:ocidListPDFArtBox forBox:(objMe's kPDFDisplayBoxArtBox)

##########################################
###Crop=トリミングサイズ=表示サイズ=可視領域
-->あくまでも『画面表示』されるエリアの指定で他の値とは別
-->通常印刷されるエリアはこれ=等落としサイズが望ましい
-->A:表示用 メディアサイズと同じ
-->B:印刷用 TrimBox(仕上がり寸)と同じ
####A:表示用
ocidPdfPageObj's setBounds:ocidPdfMediaBoxDimention forBox:(objMe's kPDFDisplayBoxCropBox)
####B:印刷用
####ocidPdfPageObj's setBounds:ocidPdfTrimBoxDimention forBox:(objMe's kPDFDisplayBoxCropBox)


#####ページカウントアップ
set numCntPageNo to numCntPageNo + 1 as number
end repeat

####保存
ocidPDFDocument's writeToURL:(ocidNSUrlPath)



#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom