Archive for the 'FileMaker Pro v11' Category

06/10 FileMaker Proで書き出したTab区切りテキストファイルをExcelで読み込む

FileMaker Proで書き出したTab区切りテキストファイルをExcel 2011で読み込むAppleScriptです。

Tab区切りテキストであればとくにFileMaker Proが書き出したものでなくてもかまいません。

スクリプト名:ExcelでTab区切りテキストを読み込む
set aFile to choose file
set aFile to aFile as string

tell application "Microsoft Excel"
  –指定のTab区切りテキストファイルを読み込む
  
open text file filename aFile data type delimited with tab
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/05 FileMaker Proで表示中のレコードのデータ内容をCSV書き出し

FileMaker Pro v11で表示中(検索してレコード数をしぼりこみ中)のレコードのデータ内容をCSV書き出しするAppleScriptです。

FileMaker Proのデータベースで検索を行って、レコード数をしぼりこんで……

fm1.jpg

条件の合うレコードのみにしぼりこみ、

fm2.jpg

本Scriptを実行すると……書き出すファイル名を聞いてくるので、それに答えると、

fm3.jpg

のように、CSV書き出しを行います。最初から、書き出し先のパス情報を、「Macintosh HD:Users:Hiyoko:Desktop:test.csv」のように持っておけば、冒頭のchoose file nameでユーザーに問い合わせる必要はありません。

書き出しを行う対象フィールドは、そのときに表示されていたレイアウトに依存します。つまり、すべてのフィールドを書き出したい場合には、一時的に「すべてのフィールドが配置されているレイアウト」に表示を切り替える必要があります(このあたり、ちょっとめんどう)。

それにしても……最新のFileMaker Pro v11にAppleScriptの資料が同梱されていないとは。資料の内容もあまり分かりやすいとはいえなかったものの、まったく付属していないというのもいささか問題でしょう。

スクリプト名:FileMaker Proで表示中のレコードのデータ内容をCSV書き出し
set aNewFile to choose file name –あくまでサンプルなので。普通はあらかじめパスのテキストを組み立てて渡すべし

–FileMaker Proで現在表示中(検索して絞り込み状態)のレコードのデータを取得する
–得られるフィールドは、表示中にレイアウトに依存する
tell application “FileMaker Pro”
  tell window 1
    set dataList to every record
  end tell
end tell

saveAsCSV(dataList, aNewFile) of me

–CSV書き出し
–ただし、データ内にダブルクォートが入っていた場合に備えてのサニタイズ処理は行っていない
on saveAsCSV(aList, aPath)
  set crlfChar to (ASCII character 13) & (ASCII character 10)
  
set LF to (ASCII character 10)
  
set wholeText to “”
  
repeat with i in aList
    set aLineText to “”
    
set curDelim to AppleScript’s text item delimiters
    
set AppleScript’s text item delimiters to “\”,\”"
    
set aLineList to i as text
    
set AppleScript’s text item delimiters to curDelim
    
    
set aLineText to repChar(aLineList, return, “”) of me –データの途中に改行が入っていた場合には削除する
    
set aLineText to repChar(aLineText, LF, “”) of me –データの途中に改行が入っていた場合には削除する
    
    
set wholeText to wholeText & “\”" & aLineText & “\”" & crlfChar –行ターミネータはCR+LF
  end repeat
  
  
if (aPath as string) does not end with “.csv” then
    set bPath to aPath & “.csv” as Unicode text
  else
    set bPath to aPath as Unicode text
  end if
  
  
write_to_file(wholeText, bPath, false) of me
  
end saveAsCSV

–ファイルの追記ルーチン「write_to_file」
–追記データ、追記対象ファイル、boolean(trueで追記)
on write_to_file(this_data, target_file, append_data)
  try
    set the target_file to the target_file as text
    
set the open_target_file to open for access file target_file with write permission
    
if append_data is false then set eof of the open_target_file to 0
    
write this_data to the open_target_file starting at eof
    
close access the open_target_file
    
return true
  on error error_message
    try
      close access file target_file
    end try
    
return error_message
  end try
end write_to_file

–文字置換
on repChar(origText, targChar, repChar)
  set origText to origText as string
  
set targChar to targChar as string
  
set repChar to repChar as string
  
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to targChar
  
set tmpList to text items of origText
  
set AppleScript’s text item delimiters to repChar
  
set retText to tmpList as string
  
set AppleScript’s text item delimiters to curDelim
  
return retText
end repChar

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に