AppleScript PDFOutline

[PDFoutline]しおり boookmarkをテキストに保存


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

#!/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.6"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

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

##################################
####ドキュメントを開いているか?チェック
##################################

###ダイアログを前面に出す
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 listUTI to {"com.adobe.pdf"}
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI 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 strFileName to (ocidFilePathURL's lastPathComponent()) as text
##################
### 出力用ファイル
##################
##パスはPDFファイル名+拡張子txt
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set strSaveFileName to (strFileName & ".しおり.txt") as text
set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSaveFileName)
##保存するテキスト
set ocidOutPutText to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidOutPutText's appendString:("\n")
##################
### 本処理
##################
###PDF読み込み
set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:(ocidFilePathURL)
##################
### メタデータ
##################
#####アトリビュートを取得して
set ocidAttributes to ocidActivDoc's documentAttributes()
#キーをリストで取得して
set ocidAllKeysArray to ocidAttributes's allKeys()
repeat with itemKeysArray in ocidAllKeysArray
  #
  set strGetKey to (itemKeysArray) as text
  set ocidValue to (ocidAttributes's valueForKey:(strGetKey))
  ###日付の場合
  if strGetKey contains "Date" then
    set ocidValue to doGetDateNo({"GGyy年MM月dd日EEEE", 2})
    ###キーワードの場合
  else if strGetKey contains "Keywords" then
    set strKeyWord to ("") as text
    repeat with itemValuet in ocidValue
      ##カンマで区切りたい場合は\tを,に変更
      set ocidKeyWord to (itemValuet's stringByReplacingOccurrencesOfString:("\n") withString:("\t"))
      set strKeyWord to (strKeyWord & ocidKeyWord) as text
    end repeat
    set ocidValue to (strKeyWord) as text
  end if
  if ocidValue = (missing value) then
    set ocidValue to ("未設定") as text
  end if
  set strSetValue to (strGetKey & " : " & ocidValue & "\n") as text
(ocidOutPutText's appendString:(strSetValue))
end repeat

(ocidOutPutText's appendString:("\n"))

##################
### シオリの処理
##################
(ocidOutPutText's appendString:("しおり outline boookmark\n"))
###しおりのROOTを取得
set ocidOutLineRoot to ocidActivDoc's outlineRoot()
set ocidSetValue to ocidOutLineRoot's label
(ocidOutPutText's appendString:("Root: " & ocidSetValue & "\n"))
###無い場合は終了
if ocidOutLineRoot = missing value then
return "しおり outline boookmarkが見つかりませんでした"
end if
set numChild to ocidOutLineRoot's numberOfChildren() as integer
##
repeat with itemNumChildNo from 0 to (numChild - 1) by 1
  ##ROOTの1階層子要素
  set ocidFirstChild to (ocidOutLineRoot's childAtIndex:(itemNumChildNo))
  ##ラベル
  set strFirstChildLabel to (ocidFirstChild's label) as text
  ##インデックス
  set strFirstChildIndex to (ocidFirstChild's |index|) as text
  ##移動先
  set ocidDistination to (ocidFirstChild's destination)
  set ocidPDFPage to ocidDistination's page
  ##リンク先
  set numLinkPageNO to (ocidActivDoc's indexForPage:(ocidPDFPage))
  set numLinkPageNO to (numLinkPageNO + 1) as integer
  ##ポイント(リンク先の座標)も必要な場合はpointも処理する
  ## log ocidDistination's |point| as list
  ##テキストに保存
  set strSetVale to ("Index:" & strFirstChildIndex & "\t" & "Destination: " & numLinkPageNO & "\t" & strFirstChildLabel)
(ocidOutPutText's appendString:(strSetVale & "\n"))
  ###2階層目の子要素の有無
  set numSubChild to ocidFirstChild's numberOfChildren() as integer
  if numSubChild > 0 then
    repeat with itemNumSubChildNo from 0 to (numSubChild - 1) by 1
      ###2階層目の子要素
      set ocidSubChild to (ocidFirstChild's childAtIndex:(itemNumSubChildNo))
      ##ラベル
      set strSubChildLabel to (ocidSubChild's label) as text
      ##インデックス
      set strSubChildIndex to (ocidSubChild's |index|) as text
      ##移動先
      set ocidDistination to (ocidSubChild's destination)
      set ocidPDFPage to ocidDistination's page
      ##リンク先
      set numLinkPageNO to (ocidActivDoc's indexForPage:(ocidPDFPage))
      set numLinkPageNO to (numLinkPageNO + 1) as integer
      ##テキスト出力
      set strSetSubVale to ("Index:" & strSubChildIndex & "\t" & "Destination: " & numLinkPageNO & "\t " & strSubChildLabel)
(ocidOutPutText's appendString:("\t " & strSetSubVale & "\n"))
    end repeat
  else if numSubChild = 0 then
log "子要素終わり"
    exit repeat
  end if
  
  
end repeat

##################
### テキスト保存
##################
set listDone to ocidOutPutText's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
return





################################
# 和暦ゼロサプレス
################################
################################
# 日付 doGetDateNo(argDateFormat,argCalendarNO)
# argCalendarNO 1 NSCalendarIdentifierGregorian 西暦
# argCalendarNO 2 NSCalendarIdentifierJapanese 和暦
################################
to doGetDateNo({argDateFormat, argCalendarNO})
  ##渡された値をテキストで確定させて
  set strDateFormat to argDateFormat as text
  set intCalendarNO to argCalendarNO as integer
  ###日付情報の取得
  set ocidDate to current application's NSDate's |date|()
  ###日付のフォーマットを定義(日本語)
  set ocidFormatterJP to current application's NSDateFormatter's alloc()'s init()
  ###和暦 西暦 カレンダー分岐
  if intCalendarNO = 1 then
    set ocidCalendarID to (current application's NSCalendarIdentifierGregorian)
  else if intCalendarNO = 2 then
    set ocidCalendarID to (current application's NSCalendarIdentifierJapanese)
  else
    set ocidCalendarID to (current application's NSCalendarIdentifierISO8601)
  end if
  set ocidCalendarJP to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:(ocidCalendarID)
  set ocidTimezoneJP to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo")
  set ocidLocaleJP to current application's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX")
  ###設定
ocidFormatterJP's setTimeZone:(ocidTimezoneJP)
ocidFormatterJP's setLocale:(ocidLocaleJP)
ocidFormatterJP's setCalendar:(ocidCalendarJP)
  # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterNoStyle)
  # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterShortStyle)
  # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterMediumStyle)
  # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterLongStyle)
ocidFormatterJP's setDateStyle:(current application's NSDateFormatterFullStyle)
  ###渡された値でフォーマット定義
ocidFormatterJP's setDateFormat:(strDateFormat)
  ###フォーマット適応
  set ocidDateAndTime to ocidFormatterJP's stringFromDate:(ocidDate)
  ###テキストで戻す
  set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo

|

[PDFOutline]親要素に対して子要素を全部入れる

現時点でプレビューでの表示が期待値にならない…トホホ
Screencapture-20230401-140751


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

#!/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.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

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

##################
###ダイアログ
##################
set listUTI to {"com.adobe.pdf"}
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI 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)

##################
### 本処理
##################
###PDF読み込み
set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL
###ページ数取得
set numCntPage to ocidActivDoc's pageCount() as integer
###しおりのROOTを取得
set ocidOutLineRoot to ocidActivDoc's outlineRoot()
###無い場合は作る
if ocidOutLineRoot = missing value then
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
else
  ####ある場合は初期化する
  ocidOutLineRoot's removeFromParent()
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
end if
########################################
####親要素
########################################
###アウトライン初期化
set ocidOutLineParent to refMe's PDFOutline's alloc()'s init()
###ラベルの初期化
set strLabel to ("ページ目次") as text
set ocidLabel to (refMe's NSString's stringWithString:strLabel)
###ラベルをセット
ocidOutLineParent's setLabel:ocidLabel
###対象のドキュメントを指定
ocidOutLineParent's setDocument:ocidActivDoc
###ROOTに子要素として入れる
ocidOutLineRoot's insertChild:ocidOutLineParent atIndex:0

###カウンター初期化
set numChkPageNo to 1 as integer
set numChkPageNoJs to 0 as integer
##################
### ページ数繰り返し
##################
repeat numCntPage times
  set ocidSetPage to ocidActivDoc's pageAtIndex:numChkPageNoJs
  ########################################
  #### 子要素
  ########################################
  ###アウトライン初期化
  set ocidOutLineChild to refMe's PDFOutline's alloc()'s init()
  ###ページサイズを取得
  set ocidPageBounds to ocidSetPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox)
  set listPageSize to item 2 of ocidPageBounds as list
  set numW to item 1 of listPageSize as number
  set numH to item 2 of listPageSize as number
  #### Destinationをセット↑ここで求めた値をそのままページ上部
  set ocidPoint to refMe's NSMakePoint(numW, numH)
  set ocidSetDestination to refMe's PDFDestination's alloc()'s initWithPage:ocidSetPage atPoint:ocidPoint
  ###zoom値はお好みで
  ocidSetDestination's setZoom:0.5
  ####ActionDestinationをセット
  set ocidSetAction to refMe's PDFActionGoTo's alloc()'s initWithDestination:ocidSetDestination
  ###ラベルの初期化
  set strLabel to ("ページ:" & numChkPageNo) as text
  set ocidLabel to (refMe's NSString's stringWithString:strLabel)
  ###ラベルをセット
  ocidOutLineChild's setLabel:ocidLabel
  ###アクションをセット
  ocidOutLineChild's setAction:ocidSetAction
  ###対象のドキュメントを指定
  ocidOutLineChild's setDocument:ocidActivDoc
  ###親要素に対して子要素として入れる
  ocidOutLineParent's insertChild:ocidOutLineChild atIndex:numChkPageNoJs
  ####カウントアップ
  set numChkPageNoJs to numChkPageNoJs + 1 as integer
  set numChkPageNo to numChkPageNo + 1 as integer
end repeat
####保存
(ocidActivDoc's writeToURL:ocidFilePathURL)


return

|

[PDFOutline]親要素に対して子要素として入れる

Screencapture-20230401-133135


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

#!/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.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

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

##################
###ダイアログ
##################
set listUTI to {"com.adobe.pdf"}
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI 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)

##################
### 本処理
##################
###PDF読み込み
set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL
###ページ数取得
set numCntPage to ocidActivDoc's pageCount() as integer
###しおりのROOTを取得
set ocidOutLineRoot to ocidActivDoc's outlineRoot()
###無い場合は作る
if ocidOutLineRoot = missing value then
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
else
  ####ある場合は初期化する
  ocidOutLineRoot's removeFromParent()
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
end if

###カウンター初期化
set numChkPageNo to 1 as integer
set numChkPageNoJs to 0 as integer
##################
### ページ数繰り返し
##################
repeat numCntPage times
  ###アウトライン初期化
  set ocidOutLineParent to refMe's PDFOutline's alloc()'s init()
  ###ページ
  set ocidSetPage to ocidActivDoc's pageAtIndex:numChkPageNoJs
  ###ページサイズを取得
  set ocidPageBounds to ocidSetPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox)
  set listPageSize to item 2 of ocidPageBounds as list
  set numW to item 1 of listPageSize as number
  set numH to item 2 of listPageSize as number
  #### Destinationをセット↑ここで求めた値をそのままページ上部
  set ocidPoint to refMe's NSMakePoint(numW, numH)
  set ocidSetDestination to refMe's PDFDestination's alloc()'s initWithPage:ocidSetPage atPoint:ocidPoint
  ###zoom値はお好みで
  ocidSetDestination's setZoom:0.5
  ####ActionDestinationをセット
  set ocidSetAction to refMe's PDFActionGoTo's alloc()'s initWithDestination:ocidSetDestination
  ###ラベルの初期化
  set strLabel to ("ページ:" & numChkPageNo) as text
  set ocidLabel to (refMe's NSString's stringWithString:strLabel)
  ###ラベルをセット
  ocidOutLineParent's setLabel:ocidLabel
  ###アクションをセット
  ocidOutLineParent's setAction:ocidSetAction
  ###対象のドキュメントを指定
  ocidOutLineParent's setDocument:ocidActivDoc
  ###ROOTに子要素として入れる
  ocidOutLineRoot's insertChild:ocidOutLineParent atIndex:numChkPageNoJs
  ###############################################
  ###アウトライン初期化
  set ocidOutLineChild to refMe's PDFOutline's alloc()'s init()
  ###ページサイズを取得
  set ocidPageBounds to ocidSetPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox)
  set listPageSize to item 2 of ocidPageBounds as list
  set numW to item 1 of listPageSize as number
  set numH to item 2 of listPageSize as number
  log
  #### Destinationをセット↑ここで求めた値をそのままページ上部
  set ocidPoint to refMe's NSMakePoint(numW, numH)
  ###zoom値はお好みで
  ocidSetDestination's setZoom:0.5
  ####ActionDestinationをセット
  set ocidSetAction to refMe's PDFActionGoTo's alloc()'s initWithDestination:ocidSetDestination
  ###ラベルの初期化
  set strLabel to ("ページトップ") as text
  set ocidLabel to (refMe's NSString's stringWithString:strLabel)
  ###ラベルをセット
  ocidOutLineChild's setLabel:ocidLabel
  ###アクションをセット
  ocidOutLineChild's setAction:ocidSetAction
  ###対象のドキュメントを指定
  ocidOutLineChild's setDocument:ocidActivDoc
  ###親要素を指定
  ###親要素に対して子要素として入れる
  ocidOutLineParent's insertChild:ocidOutLineChild atIndex:0
  ####カウントアップ
  set numChkPageNoJs to numChkPageNoJs + 1 as integer
  set numChkPageNo to numChkPageNo + 1 as integer
end repeat
####保存
(ocidActivDoc's writeToURL:ocidFilePathURL)


return

|

[PDFOutline]目次の全削除

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

#!/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.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

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

##################
###ダイアログ
##################
set listUTI to {"com.adobe.pdf"}
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI 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)

##################
### 本処理
##################
###PDF読み込み
set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL
###ページ数取得
set numCntPage to ocidActivDoc's pageCount() as integer
###しおりのROOTを取得
set ocidOutLineRoot to ocidActivDoc's outlineRoot()
###無い場合は作る
if ocidOutLineRoot = missing value then
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
else
  ####ある場合は初期化する
  ocidOutLineRoot's removeFromParent()
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
end if
####保存
(ocidActivDoc's writeToURL:ocidFilePathURL)


return

|

[PDFOutline]目次の追加

Screencapture-20230331-171153


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

#!/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.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

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

##################
###ダイアログ
##################
set listUTI to {"com.adobe.pdf"}
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI 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)

##################
### 本処理
##################
###PDF読み込み
set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL
###ページ数取得
set numCntPage to ocidActivDoc's pageCount() as integer
###しおりのROOTを取得
set ocidOutLineRoot to ocidActivDoc's outlineRoot()
###無い場合は作る
if ocidOutLineRoot = missing value then
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
else
  ####ある場合は初期化する
  ocidOutLineRoot's removeFromParent()
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
end if

###カウンター初期化
set numChkPageNo to 1 as integer
set numChkPageNoJs to 0 as integer
##################
### ページ数繰り返し
##################
repeat numCntPage times
  ###アウトライン初期化
  set ocidOutLineChild to refMe's PDFOutline's alloc()'s init()
  ###ページ
  set ocidSetPage to ocidActivDoc's pageAtIndex:numChkPageNoJs
  ###ページサイズを取得
  set ocidPageBounds to ocidSetPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox)
  set listPageSize to item 2 of ocidPageBounds as list
  set numW to item 1 of listPageSize as number
  set numH to item 2 of listPageSize as number
  #### Destinationをセット↑ここで求めた値をそのままページ上部
  set ocidPoint to refMe's NSMakePoint(numW, numH)
  set ocidSetDestination to refMe's PDFDestination's alloc()'s initWithPage:ocidSetPage atPoint:ocidPoint
  ###zoom値はお好みで
  ocidSetDestination's setZoom:0.5
  ####ActionDestinationをセット
  set ocidSetAction to refMe's PDFActionGoTo's alloc()'s initWithDestination:ocidSetDestination
  ###ラベルの初期化
  set strLabel to ("ページ:" & numChkPageNo) as text
  set ocidLabel to (refMe's NSString's stringWithString:strLabel)
  ###ラベルをセット
  ocidOutLineChild's setLabel:ocidLabel
  ###アクションをセット
  ocidOutLineChild's setAction:ocidSetAction
  ###対象のドキュメントを指定
  ocidOutLineChild's setDocument:ocidActivDoc
  ###ROOTに子要素として入れる
  ocidOutLineRoot's insertChild:ocidOutLineChild atIndex:numChkPageNoJs
  ####カウントアップ
  set numChkPageNoJs to numChkPageNoJs + 1 as integer
  set numChkPageNo to numChkPageNo + 1 as integer
end repeat
####保存
(ocidActivDoc's writeToURL:ocidFilePathURL)


return

|

[Reader]しおりの追加(ページ数分)

Screencapture-20230331-171357


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

#!/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.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

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


##################################
#### 文書を開いているか?
##################################
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
      display alert "エラー:文書が選択されていません" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 10
      return "エラー:文書が選択されていません"
    end try
  end tell
end tell
##################################
#### パス取得
##################################
tell application id "com.adobe.Reader"
  activate
  set objAvtivDoc to active doc
  tell objAvtivDoc
    ####ファイルエリアス取得
    set aliasFilePath to file alias as alias
  end tell
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 isDirectory:false)

##################################
### 本処理
##################################
##PDF読み込み
set ocidActivDoc to refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL
##ページ数を数える
set numCntPage to ocidActivDoc's pageCount() as integer
##アウトライン=しおりのROOTを取得して
set ocidOutLineRoot to ocidActivDoc's outlineRoot()
###無い場合は作る
if ocidOutLineRoot = missing value then
  set ocidOutLineRoot to refMe's PDFOutline's alloc()'s init()
  ocidActivDoc's setOutlineRoot:ocidOutLineRoot
else
  ###すでにしおりがある場合
  #####ダイアログを前面に出す
  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
    display alert "すでに目次があります追加になりが?良いですか?" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 30
  end if
end if


##################################
#### 保存
##################################
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 numChkPageNo to 1 as integer
set numChkPageNoJs to 0 as integer

##################################
#### ページ分繰り返し
##################################
repeat numCntPage times
  ####しおりの要素を初期化
  set ocidOutLineChild to refMe's PDFOutline's alloc()'s init()
  ###ページオブジェクト(リンク先のページ)
  set ocidSetPage to ocidActivDoc's pageAtIndex:numChkPageNoJs
  ###ページサイズを取得
  set ocidPageBounds to ocidSetPage's boundsForBox:(refMe's kPDFDisplayBoxCropBox)
  set listPageSize to item 2 of ocidPageBounds as list
  set numW to item 1 of listPageSize as number
  set numH to item 2 of listPageSize as number
  #### Destinationをセット↑ここで求めた値をそのままページ上部
  set ocidPoint to refMe's NSMakePoint(numW, numH)
  set ocidSetDestination to refMe's PDFDestination's alloc()'s initWithPage:ocidSetPage atPoint:ocidPoint
  ###zoom値はお好みで
  ocidSetDestination's setZoom:0.5
  ####ActionDestinationをセット
  set ocidSetAction to refMe's PDFActionGoTo's alloc()'s initWithDestination:ocidSetDestination
  ###表示名  
  set strLabel to ("ページ:" & numChkPageNo) as text
  set ocidLabel to (refMe's NSString's stringWithString:strLabel)
  ocidOutLineChild's setLabel:ocidLabel
  ##アクションをセット
  ocidOutLineChild's setAction:ocidSetAction
  ###値を入れたPDFOutline項目をドキュメントルートにセット
  ocidOutLineChild's setDocument:ocidActivDoc
  ###アウトラインのROOTに子要素として追加
  ocidOutLineRoot's insertChild:ocidOutLineChild atIndex:numChkPageNoJs
  ####カウントアップ
  set numChkPageNoJs to numChkPageNoJs + 1 as integer
  set numChkPageNo to numChkPageNo + 1 as integer
end repeat

####保存
(ocidActivDoc's writeToURL:ocidFilePathURL)


##################################
#### ファイルを開く
##################################
tell application id "com.adobe.Reader"
  activate
  open aliasFilePath options "zoom=50&pagemode=bookmarks"
end tell

return


|

その他のカテゴリー

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