AppleScript Image2PDF

[PDF化]複数の画像ファイルをPDFにまとめる(解像度を72ppi化)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use framework "QuartzCore"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


#####設定項目 短辺の長さmm
set numShortSize to 210 as integer
###################################
###ポイント換算
set numMaxPtSize to numShortSize * 2.83465 as number
###小数点2桁に(ここは好みの問題)
set numMaxPtSize to ((round (numMaxPtSize * 100) rounding down) / 100) as number
###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text
###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set listAliasFilePath to (choose file strPromptText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
else
  tell current application
    activate
    set listAliasFilePath to (choose file strPromptText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
end if
#############################################
##まずはリストをファイル名順にソート
#############################################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
#############################################
#####保存先 ダイアログ
#############################################
###並び替えたリストの最初のパス
set strFirstFilePath to ocidSortedArray's objectAtIndex:0
set ocidFilePathStr to refMe's NSString's stringWithString:strFirstFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasDefaultLocation to ocidContainerDirPathURL as alias
####ファイル名を取得
set ocidFileName to ocidContainerDirPathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子
set strFileExtension to "pdf"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & ".image." & strFileExtension) as text
set strPromptText to "名前を決めてください"
set strPromptMes to "名前を決めてください"
###選んだファイルの同階層をデフォルトの場合
#set aliasDefaultLocation to ocidContainerDirPathURL as alias
###選んだファイルの一つ上の階層の場合
set ocidDefaultLocation to ocidContainerDirPathURL's URLByDeletingLastPathComponent()
set aliasDefaultLocation to ocidDefaultLocation as alias
####デスクトップの場合
##set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ファイル名ダイアログ
####実在しない『はず』なのでas «class furl»
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
  end tell
else
  tell current application
    activate
    set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
  end tell
end if
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
set ocidSaveFilePathStr to refMe's NSString's stringWithString:strSaveFilePath
set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
set ocidContainerSaveDirPath to ocidSaveFilePath's stringByDeletingLastPathComponent()
set ocidSaveFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidSaveFilePath isDirectory:false
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:strFileExtension
end if
#############################################
####本処理
#############################################
####PDFドキュメント初期化
set ocidActivDoc to refMe's PDFDocument's alloc()'s init()
###画像のファイル数 使わない画像の数でrepeatするときはこれ
set numCntAllFileNo to (ocidSortedArray count) as integer
###画像のカウンター初期化
set numFileCnt to 0 as integer
###ファイルリストを順番に処理
repeat with itemSortedArray in ocidSortedArray
  ###########ファイルから
  set strFilePath to itemSortedArray 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 ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
  ####ピクセルサイズを取得
  set ocidImageRepArray to ocidImageData's representations()
  set ocidBitmapRep to (ocidImageRepArray's objectAtIndex:0)
  set numPixW to ocidBitmapRep's pixelsWide() as integer
  set numPixH to ocidBitmapRep's pixelsHigh() as integer
  #####サイズをピクセルサイズに設定=72ppi
  set ocidNewSize to refMe's NSSize's NSMakeSize(numPixW, numPixH)
  (ocidBitmapRep's setSize:ocidNewSize)
  ##########################################
  ####新しく↑で作ったサイズで新しいイメージを初期化して
  set ocidNewImageData to (refMe's NSImage's alloc()'s initWithSize:ocidNewSize)
  ####このイメージにサイズ変更したNSBitmapImageRepを入れる
  (ocidNewImageData's addRepresentation:ocidBitmapRep)
  ###PDFページに
  set ocidPdfPage to (refMe's PDFPage's alloc()'s initWithImage:ocidNewImageData)
  ###ページに挿入する
  (ocidActivDoc's insertPage:ocidPdfPage atIndex:numFileCnt)
  ####次ページ用にカウントアップ
  set numFileCnt to numFileCnt + 1 as integer
  set ocidNewImageData to ""
  set ocidImageData to ""
  set ocidPdfPage to ""
  set ocidDistImage to ""
end repeat

####ファイルを保存する
set boolResponse to ocidActivDoc's writeToURL:ocidSaveFilePathURL


####保存先を開く
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to appSharedWorkspace's selectFile:ocidSaveFilePath inFileViewerRootedAtPath:ocidContainerSaveDirPath

|

[PDF化]複数の画像ファイルをPDFにまとめる(解像度サイズ)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use framework "QuartzCore"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


#####設定項目 短辺の長さmm
set numShortSize to 210 as integer


###################################
###ポイント換算
set numMaxPtSize to numShortSize * 2.83465 as number
###小数点2桁に(ここは好みの問題)
set numMaxPtSize to ((round (numMaxPtSize * 100) rounding down) / 100) as number
###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text
###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set listAliasFilePath to (choose file strPromptText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
else
  tell current application
    activate
    set listAliasFilePath to (choose file strPromptText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
end if
#############################################
##まずはリストをファイル名順にソート
#############################################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
#############################################
#####保存先 ダイアログ
#############################################
###並び替えたリストの最初のパス
set strFirstFilePath to ocidSortedArray's objectAtIndex:0
set ocidFilePathStr to refMe's NSString's stringWithString:strFirstFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasDefaultLocation to ocidContainerDirPathURL as alias
####ファイル名を取得
set ocidFileName to ocidContainerDirPathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子
set strFileExtension to "pdf"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & ".image." & strFileExtension) as text
set strPromptText to "名前を決めてください"
set strPromptMes to "名前を決めてください"
###選んだファイルの同階層をデフォルトの場合
#set aliasDefaultLocation to ocidContainerDirPathURL as alias
###選んだファイルの一つ上の階層の場合
set ocidDefaultLocation to ocidContainerDirPathURL's URLByDeletingLastPathComponent()
set aliasDefaultLocation to ocidDefaultLocation as alias
####デスクトップの場合
##set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ファイル名ダイアログ
####実在しない『はず』なのでas «class furl»
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
  end tell
else
  tell current application
    activate
    set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
  end tell
end if
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
set ocidSaveFilePathStr to refMe's NSString's stringWithString:strSaveFilePath
set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
set ocidContainerSaveDirPath to ocidSaveFilePath's stringByDeletingLastPathComponent()
set ocidSaveFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidSaveFilePath isDirectory:false
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:strFileExtension
end if
#############################################
####本処理
#############################################
####PDFドキュメント初期化
set ocidActivDoc to refMe's PDFDocument's alloc()'s init()
###画像のファイル数 使わない画像の数でrepeatするときはこれ
set numCntAllFileNo to (ocidSortedArray count) as integer
###画像のカウンター初期化
set numFileCnt to 0 as integer
###ファイルリストを順番に処理
repeat with itemSortedArray in ocidSortedArray
  ###########ファイルから
  set strFilePath to itemSortedArray 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 ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
  ###PDFページに
  set ocidPdfPage to (refMe's PDFPage's alloc()'s initWithImage:ocidImageData)
  ###ページに挿入する
  (ocidActivDoc's insertPage:ocidPdfPage atIndex:numFileCnt)
  ####次ページ用にカウントアップ
  set numFileCnt to numFileCnt + 1 as integer
  set ocidNewImageData to ""
  set ocidImageData to ""
  set ocidPdfPage to ""
  set ocidDistImage to ""
end repeat

####ファイルを保存する
set boolResponse to ocidActivDoc's writeToURL:ocidSaveFilePathURL


####保存先を開く
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to appSharedWorkspace's selectFile:ocidSaveFilePath inFileViewerRootedAtPath:ocidContainerSaveDirPath

|

[PDF化]複数の画像ファイルをPDFにまとめる(短辺サイズ指定)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use framework "QuartzCore"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()


#####設定項目 短辺の長さmm
set numShortSize to 210 as integer


###################################
###ポイント換算
set numMaxPtSize to numShortSize * 2.83465 as number
###小数点2桁に(ここは好みの問題)
set numMaxPtSize to ((round (numMaxPtSize * 100) rounding down) / 100) as number
###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text
###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set listAliasFilePath to (choose file strPromptText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
else
  tell current application
    activate
    set listAliasFilePath to (choose file strPromptText with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
end if
#############################################
##まずはリストをファイル名順にソート
#############################################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
#############################################
#####保存先 ダイアログ
#############################################
###並び替えたリストの最初のパス
set strFirstFilePath to ocidSortedArray's objectAtIndex:0
set ocidFilePathStr to refMe's NSString's stringWithString:strFirstFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasDefaultLocation to ocidContainerDirPathURL as alias
####ファイル名を取得
set ocidFileName to ocidContainerDirPathURL's lastPathComponent()
####ファイル名から拡張子を取っていわゆるベースファイル名を取得
set ocidPrefixName to ocidFileName's stringByDeletingPathExtension
###ファイル名
set strPrefixName to ocidPrefixName as text
###拡張子
set strFileExtension to "pdf"
###ダイアログに出すファイル名
set strDefaultName to (strPrefixName & ".image." & strFileExtension) as text
set strPromptText to "名前を決めてください"
set strPromptMes to "名前を決めてください"
###選んだファイルの同階層をデフォルトの場合
#set aliasDefaultLocation to ocidContainerDirPathURL as alias
###選んだファイルの一つ上の階層の場合
set ocidDefaultLocation to ocidContainerDirPathURL's URLByDeletingLastPathComponent()
set aliasDefaultLocation to ocidDefaultLocation as alias
####デスクトップの場合
##set aliasDefaultLocation to (path to desktop folder from user domain) as alias
####ファイル名ダイアログ
####実在しない『はず』なのでas «class furl»
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
  end tell
else
  tell current application
    activate
    set aliasSaveFilePath to (choose file name strPromptMes default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
  end tell
end if
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
set ocidSaveFilePathStr to refMe's NSString's stringWithString:strSaveFilePath
set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
set ocidContainerSaveDirPath to ocidSaveFilePath's stringByDeletingLastPathComponent()
set ocidSaveFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidSaveFilePath isDirectory:false
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not strFileExtension then
  set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:strFileExtension
end if
#############################################
####本処理
#############################################
####PDFドキュメント初期化
set ocidActivDoc to refMe's PDFDocument's alloc()'s init()
###画像のファイル数 使わない画像の数でrepeatするときはこれ
set numCntAllFileNo to (ocidSortedArray count) as integer
###画像のカウンター初期化
set numFileCnt to 0 as integer
###ファイルリストを順番に処理
repeat with itemSortedArray in ocidSortedArray
  ###########ファイルから
  set strFilePath to itemSortedArray 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 ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
  set ocidImageSize to ocidImageData's |size|()
  set numW to ocidImageSize's width
  set numH to ocidImageSize's height
  ####短辺を指定したmmMAXで設定
  if numW > numH then
    log "横方"
    set numNewH to numMaxPtSize as number
    set numPerHW to numNewH / numH as number
    set numNewW to numW * numPerHW as number
  else if numW < numH then
    log "縦型"
    set numNewW to numMaxPtSize as number
    set numPerHW to numNewW / numW as number
    set numNewH to numH * numPerHW as number
  else
    log "正方形"
    set numNewW to numMaxPtSize as number
    set numPerHW to numNewW / numW as number
    set numNewH to numH * numPerHW as number
  end if
  set ocidNewSize to refMe's NSSize's NSMakeSize(numNewW, numNewH)
  ###中間ファイルとしてのTIFFに変換
  set ocidDistImage to ocidImageData's TIFFRepresentation()
  set ocidBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:ocidDistImage)
  ##NSBitmapImageRepのサイズを変更
  (ocidBmpImageRep's setSize:ocidNewSize)
  ####新しく↑で作ったサイズで新しいイメージを初期化して
  set ocidNewImageData to (refMe's NSImage's alloc()'s initWithSize:ocidNewSize)
  ####このイメージにサイズ変更したNSBitmapImageRepを入れる
  (ocidNewImageData's addRepresentation:ocidBmpImageRep)
  ###PDFページに
  set ocidPdfPage to (refMe's PDFPage's alloc()'s initWithImage:ocidNewImageData)
  ###ページに挿入する
  (ocidActivDoc's insertPage:ocidPdfPage atIndex:numFileCnt)
  ####次ページ用にカウントアップ
  set numFileCnt to numFileCnt + 1 as integer
  set ocidNewImageData to ""
  set ocidImageData to ""
  set ocidPdfPage to ""
end repeat

####ファイルを保存する
set boolResponse to ocidActivDoc's writeToURL:ocidSaveFilePathURL


####保存先を開く
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolSelectFileResults to appSharedWorkspace's selectFile:ocidSaveFilePath inFileViewerRootedAtPath:ocidContainerSaveDirPath

|

[単ページ]イメージファイルをPDFに変換(短辺サイズ指定)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use framework "QuartzCore"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()

#####設定項目 短辺の長さmm
set numShortSize to 210 as integer


###################################
###ポイント換算
set numMaxPtSize to numShortSize * 2.83465 as number
###小数点2桁に(ここは好みの問題)
set numMaxPtSize to ((round (numMaxPtSize * 100) rounding down) / 100) as number

###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text

###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
    set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
else
  tell current application
    activate
    set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
end if

#############################################
##まずはリストをファイル名順にソート
#############################################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
#############################################
#####保存先 ダイアログ
#############################################
try
  tell application "Finder"
    set aliasResponse to (choose folder "画像を書き出すフォルダを選択してください" with prompt "画像を書き出すフォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
  end tell
on error
  log "エラーしました"
  return
end try
set strFilePath to POSIX path of aliasResponse as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
#############################################
####本処理
#############################################

repeat with itemSortedArray in ocidSortedArray
  ###########ファイルから
  set ocidFilePathStr to (refMe's NSString's stringWithString:itemSortedArray)
  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 strBaseFileName to ocidFileName's stringByDeletingPathExtension() as text
  ###出力用のファイル名
  set strSaveFileName to strBaseFileName & ".pdf"
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:strSaveFileName)
  
  ##########################################
  ####PDFドキュメント初期化
  set ocidActivDoc to refMe's PDFDocument's alloc()'s init()
  ####入力イメージ読み込み
  set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
  set ocidImageSize to ocidImageData's |size|()
  set numW to ocidImageSize's width
  set numH to ocidImageSize's height
  ###################################
  if numW > numH then
    log "横方"
    set numNewH to numMaxPtSize as number
    set numPerHW to numNewH / numH as number
    set numNewW to numW * numPerHW as number
  else if numW < numH then
    log "縦型"
    set numNewW to numMaxPtSize as number
    set numPerHW to numNewW / numW as number
    set numNewH to numH * numPerHW as number
  else
    log "正方形"
    set numNewW to numMaxPtSize as number
    set numPerHW to numNewW / numW as number
    set numNewH to numH * numPerHW as number
  end if
  set ocidNewSize to refMe's NSSize's NSMakeSize(numNewW, numNewH)
  ###中間ファイルとしてのTIFFに変換
  set ocidDistImage to ocidImageData's TIFFRepresentation()
  set ocidBmpImageRep to (refMe's NSBitmapImageRep's imageRepWithData:ocidDistImage)
  ##NSBitmapImageRepのサイズを変更
  (ocidBmpImageRep's setSize:ocidNewSize)
  ####新しく↑で作ったサイズで新しいイメージを初期化して
  set ocidNewImageData to (refMe's NSImage's alloc()'s initWithSize:ocidNewSize)
  ####このイメージにサイズ変更したNSBitmapImageRepを入れる
  (ocidNewImageData's addRepresentation:ocidBmpImageRep)
  
  set ocidPdfPage to (refMe's PDFPage's alloc()'s initWithImage:ocidNewImageData)
  ###ページに挿入する
  (ocidActivDoc's insertPage:ocidPdfPage atIndex:0)
  ####ファイルを保存する
  set boolResponse to (ocidActivDoc's writeToURL:ocidSaveFilePathURL)
  
  ###各種解放
  set ocidImageData to ""
  set ocidActivDoc to ""
  set ocidPdfPage to ""
end repeat

####保存先を開く
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
appSharedWorkspace's openURL:ocidSaveDirPathURL





|

[単ページ]イメージファイルをPDFに変換(画像の解像度サイズ)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use framework "QuartzCore"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()

###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text
###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
else
  tell current application
    set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
end if

#############################################
##まずはリストをファイル名順にソート
#############################################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
#############################################
#####保存先 ダイアログ
#############################################
try
  set aliasResponse to (choose folder "画像を書き出すフォルダを選択してください" with prompt "画像を書き出すフォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
on error
  log "エラーしました"
  return
end try
set strFilePath to POSIX path of aliasResponse as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
#############################################
####本処理
#############################################

repeat with itemSortedArray in ocidSortedArray
  ###########ファイルから
  set ocidFilePathStr to (refMe's NSString's stringWithString:itemSortedArray)
  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 strBaseFileName to ocidFileName's stringByDeletingPathExtension() as text
  ###出力用のファイル名
  set strSaveFileName to strBaseFileName & ".pdf"
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:strSaveFileName)
  
  ##########################################
  ####入力イメージ読み込み
  set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
  ####PDFドキュメント初期化
  set ocidActivDoc to refMe's PDFDocument's alloc()'s init()
  set ocidPdfPage to (refMe's PDFPage's alloc()'s initWithImage:ocidImageData)
  ###ページに挿入する
  (ocidActivDoc's insertPage:ocidPdfPage atIndex:0)
  ####ファイルを保存する
  set boolResponse to (ocidActivDoc's writeToURL:ocidSaveFilePathURL)
  
  ###各種解放
  set ocidImageData to ""
  set ocidActivDoc to ""
  set ocidPdfPage to ""
end repeat



|

[単ページ]イメージファイルをPDFに変換(72ppi換算サイズで作成)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "PDFKit"
use framework "Quartz"
use framework "QuartzCore"
use scripting additions

property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()




###################################
#####ファイル選択ダイアログ
###################################
###ダイアログのデフォルト
set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
set listChooseFileUTI to {"public.image"}
set strPromptText to "画像ファイルを選んでください" as text
###ダイアログを前面に出す
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
else
  tell current application
    set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with multiple selections allowed without invisibles and showing package contents) as list
  end tell
end if
#############################################
##まずはリストをファイル名順にソート
#############################################
###初期化
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  (ocidFilePathArray's addObject:strFilePath)
end repeat
###ファイルリストをソート
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
#############################################
#####保存先 ダイアログ
#############################################
try
  set aliasResponse to (choose folder "画像を書き出すフォルダを選択してください" with prompt "画像を書き出すフォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as alias
on error
  log "エラーしました"
  return
end try
set strFilePath to POSIX path of aliasResponse as text
set ocidFilePathStr to refMe's NSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false
#############################################
####本処理
#############################################

repeat with itemSortedArray in ocidSortedArray
  ###########ファイルから
  set ocidFilePathStr to (refMe's NSString's stringWithString:itemSortedArray)
  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 strBaseFileName to ocidFileName's stringByDeletingPathExtension() as text
  ###出力用のファイル名
  set strSaveFileName to strBaseFileName & ".pdf"
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:strSaveFileName)
  ##########################################
  ####入力イメージ読み込み
  set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:ocidFilePathURL)
  ####サイズ取得
  set ocidImageSize to ocidImageData's |size|()
  set numW to ocidImageSize's width
  set numH to ocidImageSize's height
  ####ピクセルサイズを取得
  set ocidImageRepArray to ocidImageData's representations()
  set ocidBitmapRep to (ocidImageRepArray's objectAtIndex:0)
  set numPixW to ocidBitmapRep's pixelsWide() as integer
  set numPixH to ocidBitmapRep's pixelsHigh() as integer
  #####サイズをピクセルサイズに設定=72ppi
  set ocidNewSize to refMe's NSSize's NSMakeSize(numPixW, numPixH)
  (ocidBitmapRep's setSize:ocidNewSize)
  ##########################################
  ####新しく↑で作ったサイズで新しいイメージを初期化して
  set ocidNewImageData to (refMe's NSImage's alloc()'s initWithSize:ocidNewSize)
  ####このイメージにサイズ変更したNSBitmapImageRepを入れる
  (ocidNewImageData's addRepresentation:ocidBitmapRep)
  ####PDFドキュメント初期化
  set ocidActivDoc to refMe's PDFDocument's alloc()'s init()
  set ocidPdfPage to (refMe's PDFPage's alloc()'s initWithImage:ocidNewImageData)
  ###ページに挿入する
  (ocidActivDoc's insertPage:ocidPdfPage atIndex:0)
  ####ファイルを保存する
  set boolResponse to (ocidActivDoc's writeToURL:ocidSaveFilePathURL)
  
  ###各種解放
  set ocidBitmapRep to ""
  set ocidNewImageData to ""
  set ocidImageData to ""
  set ocidActivDoc to ""
  set ocidPdfPage to ""
end repeat

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