Acrobat Form

PDFドキュメントのフォームフィールドの情報の収集


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

#!/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 scripting additions

property refMe : a reference to current application

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

###################################
#####設定項目
###################################
##収集したデータの保存ファイル名
set strDefaultFileName to "PDFフォーム.tsv" as text
set strTargetExtension to "tsv"
###################################
#####入力
###################################
###デスクトップ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
##################
#ダイアログを全面に
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 listUTI to {"com.adobe.pdf"} as list
###ダイアログテキスト
set strPromptText to "PDFを選んでください"
try
  set aliasFilePath to (choose file strPromptText with prompt strPromptText default location aliasDesktopDirPath of type listUTI without multiple selections allowed, invisibles and showing package contents) as alias
on error
log "エラーしました"
return
end try
###パス
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 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 strPromptText to "名前を決めてください" as text
set strMesText to "名前を決めてください" as text
###ファイル名 ダイアログ
set aliasSaveFilePath to (choose file name strMesText default location aliasDesktopDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
###パス
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 ocidSaveFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false


###########################
####acrobat起動
tell application id "com.adobe.Acrobat.Pro"
activate
end tell
###########################
####起動チェック
tell application id "com.adobe.Acrobat.Pro"
  ####起動確認最大5秒
  repeat 5 times
activate
    set boolFrontmost to frontmost as boolean
    if boolFrontmost is true then
      exit repeat
    end if
delay 1
  end repeat
end tell
#####################
#####本処理
####出力用テキストの初期化
set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)

####ファイルを開く
tell application id "com.adobe.Acrobat.Pro"
  try
do script "app.openDoc(\"" & strFilePath & "\");"
  on error
open file (POSIX file strFilePath as alias)
  end try
end tell

#####################
#オープン待ち
tell application id "com.adobe.Acrobat.Pro"
  ####起動確認最大5秒
  repeat 10 times
activate
    set numCntOpenDoc to (count of every document) as integer
    if numCntOpenDoc = 0 then
delay 0.5
    else
      exit repeat
    end if
  end repeat
end tell

###########################
####開いたファイルのフォーム情報を取得する
tell application id "com.adobe.Acrobat.Pro"
  ###フォームの数を数える
do script "var numCntFild = this.numFields"
  set numCntFild to (do script " this.numFields") as text
log "フィールド数:" & numCntFild
end tell
set numCntChkFild to 0 as integer
repeat numCntFild times
  tell application id "com.adobe.Acrobat.Pro"
    ######順番にフォーム名を取得
    # do script "var strFildTitle = this.getNthFieldName(" & numCntChkFild & ").value;"
    set strFiledTitle to (do script "this.getNthFieldName(" & numCntChkFild & ")") as text
log "フィールド名:" & strFiledTitle
    if strFiledTitle is ("undefined") then
      set strFiledTitle to "" as text
    end if
ocidSaveString's appendString:(strFiledTitle)
ocidSaveString's appendString:("\t")
    ######フォームの内容を取得
    # do script "var strGetFormValue = this.getField(\"" & strFiledTitle & "\").value;"
    set strFormValeText to (do script "this.getField(\"" & strFiledTitle & "\").value;") as text
    if strFormValeText is ("undefined") then
      set strFormValeText to "" as text
    end if
log "フォームの値:" & strFormValeText
ocidSaveString's appendString:(strFormValeText)
ocidSaveString's appendString:("\n")
  end tell
  set numCntChkFild to numCntChkFild + 1 as integer
end repeat

set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
if (item 1 of listDone) = true then
log "TSVファイルに保存しました"
else
log "失敗しました"
end if




|

【フォームデータ収集】XMLなFDF (XML形式とXDFDの違い)

フォームデータの収集です。
注釈データではありませんので留意ください
サンプルデータ

ダウンロード - このままデスクトップへ.zip


XML形式はフラットなリスト構造

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

<?xml version="1.0" encoding="UTF-8"?>
<fields xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">
<SOME_FORM_A>AAAAAAAAAAAAAAAAAA</SOME_FORM_A>
<SOME_FORM_B>BBBBBBBBBBBBBBBB</SOME_FORM_B>
</fields>


XFDFはリスト構造がやや複雑

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

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<f href="FILE_NAME.pdf"/>
<fields>
<field name="SOME_FORM_A">
<value>AAAAAAAAAAAAAAAAAA</value>
</field>
<field name="SOME_FORM_B">
<value>BBBBBBBBBBBBBBBB</value>
</field>
</fields>
</xfdf>

XMLなFDFの取得

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

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

property refMe : a reference to current application


#######【1】URL

set strFilePath to "~/Desktop/XMLなFDF.xml" 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)


#######【2】NSXMLDocument
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###ROOT
set ocidRootElement to ocidReadXMLDoc's rootElement()
set numChild to (count of ocidRootElement's children) as integer
###第一階層だけの子要素
repeat with numCntChild from 0 to (numChild - 1)
  log (ocidRootElement's childAtIndex:numCntChild)'s |name| as text
  log (ocidRootElement's childAtIndex:numCntChild)'s stringValue as text
end repeat

-->出力
(*SOME_FORM_A*)
(*AAAAAAAAAAAAAAAAAA*)
(*SOME_FORM_B*)
(*BBBBBBBBBBBBBBBB*)

XFDFはattribute有り

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

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

property refMe : a reference to current application


#######【1】URL

set strFilePath to "~/Desktop/XMLなFDF.xfdf" 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)


#######【2】NSXMLDocument
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###ROOT
set ocidRootElement to ocidReadXMLDoc's rootElement()
###ファイル名部
set ocidChilds to ocidRootElement's elementsForName:"f"
set ocidFileName to (ocidChilds's objectAtIndex:0)
set ocidAttribute to (ocidFileName's attributeForName:"href")
log ocidAttribute's stringValue as text

###fields
set ocidChilds to ocidRootElement's elementsForName:"fields"
set ocidElementFields to (ocidChilds's objectAtIndex:0)
###子要素 構造的にはArray
set ocidChilds to ocidElementFields's elementsForName:"field"
set numChild to (count of ocidChilds's children) as integer

###第一階層だけの子要素
repeat with numCntChild from 0 to (numChild - 1)
  set ocidElementField to (ocidChilds's objectAtIndex:(numCntChild))
  set ocidAttribute to (ocidElementField's attributeForName:"name")
  log ocidAttribute's stringValue as text
  set ocidValue to (ocidElementField's elementsForName:("value"))
  log ocidValue's stringValue as text
end repeat
-->出力
(*FILE_NAME.pdf*)
(*SOME_FORM_A*)
(*AAAAAAAAAAAAAAAAAA*)
(*SOME_FORM_B*)
(*BBBBBBBBBBBBBBBB*)

|

[FORM]メール文書を作成中にエラーが発生したので、Adobe Acrobat は要求を完了することができませんでした。

エラーメッセージが
『メール文書を作成中にエラーが発生したので、Adobe Acrobat は要求を完了することができませんでした。次の手順に進む方法がわからない場合は、フォームを保存し、インターネット電子メールサービスを使用してそのフォームを手動で返信することができます。』
なら

システム設定>>プライバシーとセキュリティ>>オートメーション

Acrobatがメールを許可しているかです

Screencapture-20230526-201635

↑これを
↓こうね
Screencapture-20230526-211607

|

フォームベーシック(数値)

ダウンロード - acr6842168065282355621.tmp.pdf


Screencapture_20230515_11_08_09
Screencapture_20230515_11_00_31
Screencapture_20230515_11_00_45
Screencapture_20230515_11_20_50
Screencapture_20230515_11_01_01

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

//フォームの値を取得
var numGetNumber = event.value;
//数値に確定
numTypeofNumber = parseInt(numGetNumber);
//デフォルト値のままだったら
if (numGetNumber == 999) {
  this.getField("numNumber3Digits").fillColor = color.red;
  this.getField("numNumber3Digits").strokeColor = color.red;
//数値以外なら
}else if (!(typeof numTypeofNumber === "number")){
  this.getField("numNumber3Digits").fillColor = color.red;
  this.getField("numNumber3Digits").strokeColor = color.red;
//文字数が3桁あれば
} else if (/^\d{3}$/.test(numGetNumber.toString())) {
  this.getField("numNumber3Digits").fillColor = color.transparent;
  this.getField("numNumber3Digits").strokeColor = color.black;
//それ以外なら
} else {
  this.getField("numNumber3Digits").fillColor = color.red;
  this.getField("numNumber3Digits").strokeColor = color.red;
}

|

[開発用]フォームの名前をフォームの値に入れる

これを
Screencapture-20230423-122023
こうします
Screencapture-20230423-122011

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

#!/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 scripting additions

property refMe : a reference to current application

tell application id "com.adobe.Reader"
  tell active doc
    ###フィールドの数を数えて
    do script "var numFieldCnt = this.numFields;"
    set numFieldCnt to (do script "this.numFields;") as integer
    set numFieldCntJS to numFieldCnt
    ####フィールドの数だけ繰り返し
    repeat numFieldCnt times
      set numFieldCntJS to numFieldCntJS - 1 as integer
      ####名前を取得して
      do script "var strFieldName = this.getNthFieldName(" & numFieldCntJS & ");"
      set strFieldName to (do script "this.getNthFieldName(" & numFieldCntJS & ");") as text
      ####リードオンリーを解除して
      do script "this.getField(\"" & strFieldName & "\").readonly = false;"
      ###値を入れる
      do script " this.getField(strFieldName).value = strFieldName;"
    end repeat
  end tell
end tell


|

[Form]フォームの内容を読取専用にする

読取専用へ

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

#!/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 scripting additions


property refMe : a reference to current application

set listFormName to {} as list


##################################
#### 文書を開いているか?
##################################
tell application id "com.adobe.Reader"
  ###PROで利用したい場合はこちら
  ##tell application id "com.adobe.Acrobat.Pro"
  activate
  tell active doc
    ###javascript無効になっているとここでエラーになる
    set numAllPage to do script ("this.numPages;")
    set numNowPage to do script ("this.pageNum;")
    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"
  ###PROで利用したい場合はこちら
  ##tell application id "com.adobe.Acrobat.Pro"
  tell active doc
    ###フィールドの数を数えて
    do script "var numFieldCnt = this.numFields;"
    set numFieldCnt to (do script "this.numFields;") as integer
    set numFieldCntJS to numFieldCnt
    ####フィールドの数だけ繰り返し
    repeat numFieldCnt times
      set numFieldCntJS to numFieldCntJS - 1 as integer
      ####名前を取得して
      do script "var strFildTitle = this.getNthFieldName(" & numFieldCntJS & ");"
      set strFieldName to (do script "this.getNthFieldName(" & numFieldCntJS & ");") as text
      ######フォームの内容をAppleScriptに渡す
      set strFormValue to (do script "this.getField(strFildTitle).value;") as text
      ####タブ区切りに値を整形して
      set listFormNameAndValue to ("" & strFieldName & "\t" & strFormValue & "") as text
      ###リストに追加する
      copy listFormNameAndValue to end of listFormName
    end repeat
  end tell
end tell

##################################
#### ダイアログ
##################################
###ダイアログを前面に出す
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

try
  tell current application
    activate
    set listResponse to (choose from list listFormName with title "選んでください" with prompt "選択した入力欄を読取専用に変更します" default items (item 1 of listFormName) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list
  end tell
on error
  log "エラーしました"
  return "エラーしました"
  error "エラーしました" number -200
end try

if (item 1 of listResponse) is false then
  return "キャンセルしました"
  error "キャンセルしました" number -200
end if
##################################
#### 本処理
##################################

repeat with itemResponse in listResponse
  ###受け取った戻り値はタブ区切りなので
  set strFormNameAndValue to itemResponse as text
  set AppleScript's text item delimiters to "\t"
  set listFormNameAndValue to every text item of strFormNameAndValue
  set AppleScript's text item delimiters to ""
  set strFormName to item 1 of listFormNameAndValue
  
  tell application id "com.adobe.Reader"
    ###PROで利用したい場合はこちら
    ##tell application id "com.adobe.Acrobat.Pro"
    tell active doc
      ####リードオンリーにする
      do script "this.getField(\"" & strFormName & "\").readonly = true;"
    end tell
  end tell
end repeat




解除

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

#!/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 scripting additions


property refMe : a reference to current application

set listFormName to {} as list


##################################
#### 文書を開いているか?
##################################
tell application id "com.adobe.Reader"
  ###PROで利用したい場合はこちら
  ##tell application id "com.adobe.Acrobat.Pro"
  activate
  tell active doc
    ###javascript無効になっているとここでエラーになる
    set numAllPage to do script ("this.numPages;")
    set numNowPage to do script ("this.pageNum;")
    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"
  ###PROで利用したい場合はこちら
  ##tell application id "com.adobe.Acrobat.Pro"
  tell active doc
    ###フィールドの数を数えて
    do script "var numFieldCnt = this.numFields;"
    set numFieldCnt to (do script "this.numFields;") as integer
    set numFieldCntJS to numFieldCnt
    ####フィールドの数だけ繰り返し
    repeat numFieldCnt times
      set numFieldCntJS to numFieldCntJS - 1 as integer
      ####名前を取得して
      do script "var strFildTitle = this.getNthFieldName(" & numFieldCntJS & ");"
      set strFieldName to (do script "this.getNthFieldName(" & numFieldCntJS & ");") as text
      ######フォームの内容をAppleScriptに渡す
      set strFormValue to (do script "this.getField(strFildTitle).value;") as text
      ####タブ区切りに値を整形して
      set listFormNameAndValue to ("" & strFieldName & "\t" & strFormValue & "") as text
      ###リストに追加する
      copy listFormNameAndValue to end of listFormName
    end repeat
  end tell
end tell

##################################
#### ダイアログ
##################################
###ダイアログを前面に出す
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

try
  tell current application
    activate
    set listResponse to (choose from list listFormName with title "選んでください" with prompt "選択した入力欄を読取専用に変更します" default items (item 1 of listFormName) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list
  end tell
on error
  log "エラーしました"
  return "エラーしました"
  error "エラーしました" number -200
end try

if (item 1 of listResponse) is false then
  return "キャンセルしました"
  error "キャンセルしました" number -200
end if
##################################
#### 本処理
##################################

repeat with itemResponse in listResponse
  ###受け取った戻り値はタブ区切りなので
  set strFormNameAndValue to itemResponse as text
  set AppleScript's text item delimiters to "\t"
  set listFormNameAndValue to every text item of strFormNameAndValue
  set AppleScript's text item delimiters to ""
  set strFormName to item 1 of listFormNameAndValue
  
  tell application id "com.adobe.Reader"
    ###PROで利用したい場合はこちら
    ##tell application id "com.adobe.Acrobat.Pro"
    tell active doc
      ####リードオンリーを解除
      do script "this.getField(\"" & strFormName & "\").readonly = false;"
    end tell
  end tell
end repeat

|

[Form]リストの内容でPDFを作成して、回収後にデータ収集するまで

ダウンロード - 配布用PDF作成回収.zip

|

[Form]回収したPDFからフォームの内容を集計する

過去に作成したものの修正版
https://force4u.cocolog-nifty.com/skywalker/2021/09/post-1db92c.html

ダウンロード -回収PDFデータ収集.zip


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

#!/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 scripting additions

property refMe : a reference to current application

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

###################################
#####設定項目
###################################
##収集したデータの保存ファイル名
set strFileName to "PDFフォーム集計データ.txt" as text


###################################
#####入力フォルダ
###################################
###ダイアログのデフォルト
tell application "Finder"
  set aliasPathToMe to path to me as alias
  set aliasContainerPath to container of aliasPathToMe as alias
  #####回収したPDFフォームを集めてあるフォルダ名
  set aliasDefaultLocation to (folder "回収PDFサンプル" of folder aliasContainerPath) as alias
end tell
#####空のテキストファイルを作っておく
tell application "Finder"
  try
    make new file at aliasContainerPath with properties {name:(strFileName)}
  end try
end tell

##################################################
#####回収PDFのパス関連
##################################################

###ダイアログテキスト
set strPromptText to "回収したPDFフォルダを選んでください"
try
  set listResponse to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as list
on error
  log "エラーしました"
  return
end try
###フォルダーのパス関連
set aliasDirPath to (item 1 of listResponse) as alias
set strDirPath to POSIX path of aliasDirPath as text
set ocidDirPathStr to refMe's NSString's stringWithString:strDirPath
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath
set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidDirPath isDirectory:true

###フォルダに内包されているコンテンツ
set listArray to {(refMe's NSURLIsDirectoryKey), (refMe's NSURLIsRegularFileKey), (refMe's NSURLPathKey)}
set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
set listFilePathArray to appFileManager's contentsOfDirectoryAtURL:ocidDirPathURL includingPropertiesForKeys:listArray options:ocidOption |error|:(reference)
set ocidFilePathArray to item 1 of listFilePathArray

###パス格納用のリスト
set ocidPathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###URLリストをパスリストにして(ディレクトリ等を除く処理)
repeat with itemFilePath in ocidFilePathArray
  set boolIsRegFile to (item 2 of (itemFilePath's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))) as boolean
  set boolIsDir to (item 2 of (itemFilePath's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))) as boolean
  if boolIsDir is false then
    if boolIsRegFile is true then
      set ocidFilePath to item 2 of (itemFilePath's getResourceValue:(reference) forKey:(refMe's NSURLPathKey) |error|:(reference))
      (ocidPathArray's addObject:ocidFilePath)
    end if
  end if
end repeat
#####(同じフォルダ内だから)ファイル名順にソート
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"localizedStandardCompare:"
set ocidSortedPathList to (ocidPathArray's sortedArrayUsingDescriptors:{ocidDescriptor})
##################################################
#####準備
##################################################
###########################
####acrobat起動
tell application id "com.adobe.Acrobat.Pro"
  launch
end tell
###########################
####起動チェック
tell application id "com.adobe.Acrobat.Pro"
  ####起動確認最大5秒
  repeat 5 times
    activate
    set boolFrontmost to frontmost as boolean
    if boolFrontmost is true then
      exit repeat
    end if
    delay 1
  end repeat
end tell

##################################################
#####本処理
##################################################
####出力用テキストの初期化
set strOutPutTextTab to "" as text

###ファイルの数だけ繰り返し
repeat with itemSortedPath in ocidSortedPathList
  set strFilePath to itemSortedPath as text
  ####ファイルを開く
  tell application id "com.adobe.Acrobat.Pro"
    try
      do script "app.openDoc(\"" & strFilePath & "\");"
    on error
      open file (POSIX file strFilePath as alias)
    end try
  end tell
  ####開いたファイルのフォーム情報を取得する
  tell application id "com.adobe.Acrobat.Pro"
    ###フォームの数を数える
    do script "var numCntFild = this.numFields"
    ###AppleScript用にテキストにして戻す
    set numCntFild to (do script "numCntFild") as text
  end tell
  set numCntChkFild to 0 as integer
  repeat numCntFild times
    tell application id "com.adobe.Acrobat.Pro"
      ######順番にフォーム名を取得
      do script "var strFildTitle = this.getNthFieldName(" & numCntChkFild & ")"
      ######フォームの内容を取得
      do script "var strFormValeText = this.getField(strFildTitle).value;"
      ######フォームの内容をAppleScriptに渡す
      set strFormValue to (do script "this.getField(strFildTitle).value;") as text
    end tell
    ###改行除去
    set strFormValue to doReplace(strFormValue, "\r", "")
    set strFormValue to doReplace(strFormValue, "\n", "")
    ######フォーム毎にタブ区切りテキストにする
    set strOutPutTextTab to strOutPutTextTab & strFormValue & "\t" as text
    set numCntChkFild to numCntChkFild + 1 as integer
  end repeat
  ####1ファイル毎に改行を入れる
  set strOutPutTextTab to strOutPutTextTab & "\n" as text
  
  ####開いたファイルを閉じる
  tell application id "com.adobe.Acrobat.Pro"
    do script "closeDoc(false);"
  end tell
end repeat

log strOutPutTextTab

####結果テキスト
tell application "Finder"
  set aliasFilePath to (file strFileName of folder aliasContainerPath) as alias
end tell
tell application "TextEdit"
  activate
  set objAvtivDoc to open file aliasFilePath
  tell objAvtivDoc
    activate
    set its text to strOutPutTextTab
    save in aliasFilePath
  end tell
end tell

####文字の置き換え
to doReplace(argOrignalText, argSearchText, argReplaceText)
  set strDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to argSearchText
  set listDelim to every text item of argOrignalText
  set AppleScript's text item delimiters to argReplaceText
  set strReturn to listDelim as text
  set AppleScript's text item delimiters to strDelim
  return strReturn
end doReplace

|

[Form]配布用PDF作成

過去に作成したものの修正版
https://force4u.cocolog-nifty.com/skywalker/cat75941294/index.html
名簿データに沿った内容のPDFとファイル名を生成します。
Javascriptを利用します

ダウンロード - 配布用PDF作成.zip



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

#!/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 scripting additions

property refMe : a reference to current application

##################################################
#####設定項目
##################################################

#####読み込みデータファイル(TAB区切りテキスト)
set strDataFileName to "ユーザーデータ.tab.txt" as text

#####テンプレートファイル名
set strTemplatesFileName to "Templates.pdf" as text

#####データを代入するための
#####テンプレートPDFのフォーム名を設定
set strFormNameA to "strAdminText1" as text
set strFormNameB to "strAdminText2" as text


##################################################
#####ファイルパス関連
##################################################
#####このファイルのディレクトリ
tell application "Finder"
  set aliasPathToMe to path to me as alias
  set aliasContainerDir to (container of aliasPathToMe) as alias
end tell
####UNIXパス
set strContainerDirPath to (POSIX path of aliasContainerDir) as text
####テンプレートのあるフォルダ名
set strTemFilePath to ("" & strContainerDirPath & "PDFTemplates/" & strTemplatesFileName & "") as text
set aliasTemFilePath to POSIX file strTemFilePath as alias
####読込データのあるフォルダ名
set strDataFilePath to ("" & strContainerDirPath & "Data/" & strDataFileName & "") as text
####出力先になるフォルダ名
set strOutPutDir to ("" & strContainerDirPath & "OutPut/") as text


##################################################
#####準備
##################################################
###########################
####acrobat起動
tell application id "com.adobe.Acrobat.Pro"
  launch
end tell
###########################
####起動チェック
tell application id "com.adobe.Acrobat.Pro"
  ####起動確認最大5秒
  repeat 5 times
    activate
    set boolFrontmost to frontmost as boolean
    if boolFrontmost is true then
      exit repeat
    end if
    delay 1
  end repeat
end tell
###########################
####テンプレートオープン
tell application id "com.adobe.Acrobat.Pro"
  try
    do script "app.openDoc(\"" & strTemFilePath & "\");"
  on error
    open file aliasTemFilePath
  end try
end tell
###########################
####ファイルオープンチェック
tell application id "com.adobe.Acrobat.Pro"
  activate
  ####ファイルオープン待ち時間5秒
  tell front PDF Window
    repeat 5 times
      try
        set boolvisible to visible as boolean
        if boolvisible is true then
          exit repeat
        end if
      on error
        delay 1
      end try
    end repeat
  end tell
end tell


##########################################
###タブ区切りテキストデータ読込
set ocidTeFilePathStr to refMe's NSString's stringWithString:strDataFilePath
set ocidFilePath to ocidTeFilePathStr's stringByStandardizingPath
set listReadData to refMe's NSString's stringWithContentsOfFile:ocidFilePath encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
set ocidReadData to item 1 of listReadData
###改行毎にリスト化
set ocidReadDataArray to ocidReadData's componentsSeparatedByString:"\n"
if (count of ocidReadDataArray) = 1 then
  set ocidReadDataArray to ocidReadData's componentsSeparatedByString:"\r"
  if (count of ocidReadDataArray) = 1 then
    set ocidReadDataArray to ocidReadData's componentsSeparatedByString:"\r\n"
  end if
end if
#######################
###ファイル毎のリストの初期化
set ocidSetArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemReadDataArray in ocidReadDataArray
  ###毎名分のデータはタブ区切り
  set ocidUserDataArray to (itemReadDataArray's componentsSeparatedByString:"\t")
  (ocidSetArray's addObject:ocidUserDataArray)
end repeat
###
set numCntArray to (ocidSetArray count) as integer

##################################################
#####本処理
##################################################
###読み取りリスト番号初期化(ゼロインデックスですが1行目が項目名なので1から)
set numCntLineNo to 1 as integer
###最初の行が見出しなので1引いた回数繰り返す
repeat (numCntArray - 1) times
  ####リストからX番目のデータを取り出して
  set itemSetArray to ocidSetArray's objectAtIndex:numCntLineNo
  ####取り出したデータはリストなので1番目と2番目でそれぞれ値に
  set strReadData1 to (itemSetArray's objectAtIndex:0) as text
  ####最後に改行入れちゃった場合の対策
  if strReadData1 is "" then
    exit repeat
  end if
  set strReadData2 to (itemSetArray's objectAtIndex:1) as text
  
  ##ファイル名定義
  set strSaveFileName to ("" & strReadData1 & "." & strReadData2 & ".pdf") as text
  ####出力ファイルパス
  set strOutPutFilePath to ("" & strOutPutDir & strSaveFileName & "") as text
  
  tell application id "com.adobe.Acrobat.Pro"
    
    #####フォームに値を入れる
    #theFormNameA
    do script "var strReadText1 = \"" & strReadData1 & "\";"
    ####改行を取り除く
    do script "strReadText1 = strReadText1.replace(\"\\r\",\"\");"
    do script "strReadText1 = strReadText1.replace(\"\\n\",\"\");"
    do script " this.getField(\"" & strFormNameA & "\").value = strReadText1;"
    ##do script "var strReadText1 = this.getField(\"" & strFormNameA & "\").value;"
    ###リードオンリーにする
    do script "this.getField(\"" & strFormNameA & "\").readonly = true;"
    ###################################
    #####フォームに値を入れる
    #theFormNameB
    do script "var strReadText2 = \"" & strReadData2 & "\";"
    ####改行を取り除く
    do script "strReadText2 = strReadText2.replace(\"\\r\",\"\");"
    do script "strReadText2 = strReadText2.replace(\"\\n\",\"\");"
    do script " this.getField(\"" & strFormNameB & "\").value = strReadText2;"
    ##  do script "var strReadText2 = this.getField(\"" & strFormNameB & "\").value;"
    ###リードオンリーにする
    do script "this.getField(\"" & strFormNameB & "\").readonly = true;"
    
    ###項目増やすときはここに
    ###ファイル保存
    do script "this.saveAs(\"" & strOutPutFilePath & "\");"
    ###リードオンリーを解除して次に備える
    do script "this.getField(\"" & strFormNameA & "\").readonly = false;"
    do script "this.getField(\"" & strFormNameB & "\").readonly = false;"
    ###元データに誤りがある対策で保存終わったらフォームをクリアする
    do script " this.getField(\"" & strFormNameA & "\").value = \"\";"
    do script " this.getField(\"" & strFormNameB & "\").value = \"\";"
  end tell
  
  ##カウントアップ
  set numCntLineNo to numCntLineNo + 1 as integer
end repeat

tell application id "com.adobe.Acrobat.Pro"
  close front document saving no
end tell
####出来上がりPDFフォルダを開く
set ocidShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
set ocidDirPathURL to refMe's NSURL's fileURLWithPath:strOutPutDir
ocidShardWorkspace's openURL:ocidDirPathURL


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