« [exiftool]PDFのメタデータ全部削除 | トップページ | [svg]adobe_illustrator_pgf »

[PDF]メタデータ タイトル設定


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
exiftoolが別途必要です
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-e7e51d.html

*)
# 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 strBinPath to ("/usr/local/bin/exiftool") as text --通常はこちら
set strBinPath to ("~/bin/exiftool/exiftool") as text


set objFileManager 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 ocidUserDesktopPath to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias
tell application "Finder"
end tell
set listChooseFileUTI to {"com.adobe.pdf"}
set strPromptText to "PDFファイルを選んでください" as text
set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents) as list

#######################################
## クリップボードの中身取り出し
#######################################
###初期化
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to appPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
  ###値を格納する
  tell application "Finder"
    set strReadString to (the clipboard as text) as text
  end tell
  ###Finderでエラーしたら
else
  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
  if boolContain = true then
    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set strReadString to ocidReadString as text
  else
log "テキストなし"
    set aliasFirstFilePath to (item 1 of listAliasFilePath) as alias
    tell application "Finder"
      set strFirstFileName to name of aliasFirstFilePath as text
    end tell
    set strReadString to strFirstFileName as text
  end if
end if
########################
##ダイアログを前面に出す
set strName to (name of current application) as text
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
########################
##ダイアログ
set aliasIconPath to (POSIX file "/System/Library/CoreServices/Tips.app/Contents/Resources/AppIcon.icns") as alias
set strTitle to ("入力してください") as text
set strMes to ("タイトルを入力してください\r") as text
set recordResult to (display dialog strMes with title strTitle default answer strReadString buttons {"キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer)
if (gave up of recordResult) is true then
return "時間切れです"
else if (button returned of recordResult) is "キャンセル" then
return "キャンセルです"
else
  set strReturnedText to (text returned of recordResult) as text
end if
set ocidReturnedText to refMe's NSString's stringWithString:(strReturnedText)
########################
##改行を取る
set ocidReturned to ocidReturnedText's stringByReplacingOccurrencesOfString:("\r") withString:("")
set ocidReturnedText to ocidReturned's stringByReplacingOccurrencesOfString:("\n") withString:("")
set strSetText to ocidReturnedText as string
#####BIN path
set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
set ocidBinPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:false)
set strBinPath to ocidBinPathURL's |path| as text


#######################################
###ダイアログで選択した書類の数だけ繰り返し
#######################################
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
  set strFileName to ocidFilePath's lastPathComponent() as text
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false)
  #####PDFDocumentとして読み込み
  set ocidActivDoc to (refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL)
  ########################################
  #####暗号化チェック
  set boolEncrypted to ocidActivDoc's isEncrypted()
  if boolEncrypted is true then
    set strMes to "エラー:" & strFileName & "暗号化されています" as text
display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3
return "暗号化されているので変更できません"
  end if
  ########################################
  #####ロック確認
  set boolLocked to ocidActivDoc's isLocked()
  if boolLocked is true then
    set strMes to "エラー:" & strFileName & "パスワードでロックされています" as text
display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3
return "パスワードでロックされているので変更できません"
  end if
  ########################################
  #####アトリビュートを取得して
  set ocidAttributes to ocidActivDoc's documentAttributes()
  ######可変ディクショナリに格納
  set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithDictionary:ocidAttributes)
log ocidAttrDict as record
  ###Titleキーの内容を設定
(ocidAttrDict's setValue:(ocidReturnedText) forKey:"Title")
(ocidAttrDict's setValue:"" forKey:"Creator")
  #####値を変更したレコードをセットする
(ocidActivDoc's setDocumentAttributes:ocidAttrDict)
  ##################
  ###保存
  ##################
(ocidActivDoc's writeToURL:ocidFilePathURL)
  ####解放
  set ocidActivDoc to ""
  set ocidDocAttrDict to ""
  ##################
  ### exifToolでXMPを削除
  ##################
  set strCommandText to ("\"" & strBinPath & "\" -m -overwrite_original -title=\"" & strSetText & "\" -xmp:Title=\"" & strSetText & "\" -xmp:dc:Title=\"" & strSetText & "\" -IPTC:ObjectName=\"" & strSetText & "\" \"" & strFilePath & "\"") as text
  try
do shell script strCommandText
  end try
end repeat


return "処理終了"






|

« [exiftool]PDFのメタデータ全部削除 | トップページ | [svg]adobe_illustrator_pgf »

AppleScript PDF」カテゴリの記事

AppleScript EXIFData」カテゴリの記事

AppleScript XMP」カテゴリの記事