tsvタブ区切りテキストに行番号を入れる
#!/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
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
#################################
## 設定
## 1行目に番号入れる場合はfalseを
## 1行目は項目名で 2行目からはtrue
#################################
set boolFirstLineIsTitle to true as boolean
#デフォルトの改行コード 改行の無いテキストの場合用の指定
set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
#################################
### 【1】入力ファイル
#################################
#ダイアログ
tell current application
set strName to name as text
end tell
#スクリプトメニューから実行したら
if strName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
#デフォルトロケーション
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 listUTI to {"public.tab-separated-values-text"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
### ファイル選択時
set aliasFilePath to (choose file strMes with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
if aliasFilePath is (missing value) then
return "選んでください"
end if
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)
#################################
### 【2】保存先
#################################
set ocidFileName to ocidFilePathURL's lastPathComponent()
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
set strMes to "フォルダを選んでください" as text
set strPrompt to "フォルダを選択してください\n入力フォルダと同じフォルダを選択すると\n『上書き』になります" as text
try
###ダイアログを前面に出す
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 aliasResponse to (choose folder strMes with prompt strPrompt default location aliasContainerDirPath with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try
#戻り値エイリアスをURLに
set strSaveDirPath to (POSIX path of aliasResponse) as text
set ocidSaveDirPathStr to (refMe's NSString's stringWithString:(strSaveDirPath))
set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName)
#上書きチェック
if aliasContainerDirPath = aliasResponse then
log "同じフォルダに保存しようとしています"
set strMes to "上書きを指定しました。\nファイルは上書きになります\nいいですか?" as text
try
###ダイアログを前面に出す
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
#
tell application (POSIX path of (path to frontmost application))
set refResponse to (display alert ("【注意】:" & strMes) buttons {"OK", "処理中止"} default button "OK" cancel button "処理中止" as informational giving up after 20)
end tell
on error
log "エラーしました"
return "エラーしました"
error number -128
end try
if true is equal to (gave up of refResponse) then
return "時間切れですやりなおしてください"
error number -128
end if
if "処理中止" is equal to (button returned of refResponse) then
return "処理を中止しました"
end if
end if
#################################
### 【3】本処理
#################################
#ファイル読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
if ocidReadString = (missing value) then
set strFileName to ocidFilePathURL's lastPathComponent() as text
set strMes to ("文字コードを判定出来ませんでした : " & strFileName & "\n処理を中止します") as text
display alert strMes
return
end if
##############################
#改行コード判定
set ocidCrRange to (ocidReadString's rangeOfString:("\r"))'s location()
set ocidLfRange to (ocidReadString's rangeOfString:("\n"))'s location()
if ocidCrRange = refNSNotFound then
log "Mac改行は無い"
if ocidLfRange = refNSNotFound then
log "Mac改行もUNIX改行は無い=改行の無い1行か空のテキスト"
set ocidLength to ocidReadString's |length|
if ocidLength = (refMe's NSNumber's numberWithInteger:0) then
log "空のテキスト"
set ocidNewLineChar to ocidSetNewLineChar
else
log "改行無しの1行テキスト"
set ocidNewLineChar to ocidSetNewLineChar
end if
else
log "Mac改行無+UNIX改行あり=UNIX改行"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\n"))
end if
else
if ocidLfRange = refNSNotFound then
log "MacがあるUNIX改行は無い=Mac改行"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\r"))
else
log "Mac改行ある+UNIX改行あり=Windows改行"
set ocidNewLineChar to (refMe's NSString's stringWithString:("\r\n"))
end if
end if
#改行毎でリストにする
set ocidLineArray to (ocidReadString's componentsSeparatedByString:(ocidNewLineChar))
#出力用のリスト
set ocidEditLineArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
#読み取ったデータに項目連番を入れておく
set numCntLineNo to 0 as integer
repeat with itemLineArray in ocidLineArray
set ocidEditLineText to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
if numCntLineNo = 0 then
if boolFirstLineIsTitle is true then
set strAppendText to ("NO\t") as text
else if boolFirstLineIsTitle is false then
set numCntLineNo to (numCntLineNo + 1) as integer
set strAppendText to (numCntLineNo & "\t") as text
end if
(ocidEditLineText's appendString:(strAppendText))
(ocidEditLineText's appendString:(itemLineArray))
else
if (itemLineArray as text) is "" then
exit repeat
end if
set strAppendText to (numCntLineNo & "\t") as text
(ocidEditLineText's appendString:(strAppendText))
(ocidEditLineText's appendString:(itemLineArray))
end if
(ocidEditLineArray's addObject:(ocidEditLineText))
#カウントアップ
set numCntLineNo to (numCntLineNo + 1) as integer
end repeat
##テキストに戻して
set ocidSaveStrings to ocidEditLineArray's componentsJoinedByString:(ocidNewLineChar)
#最後に改行を入れて改行で終わるタイプにする
ocidSaveStrings's appendString:(ocidNewLineChar)
#################################
### 【4】保存
#################################
set listDone to (ocidSaveStrings's writeToURL:(ocidSaveFilePathURL) atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
| 固定リンク
「Text TSV」カテゴリの記事
- ユーザー辞書作成支援スクリプト(追加辞書用テキストも作成するようにした)(2024.10.13)
- TSV2HTML TSVタブ区切りテキストをHTMLテーブルにする(少し修正)(2024.03.28)
- TSV行列入替(2024.03.28)
- TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する(1ページに複数面付け版)(2024.03.06)
- TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する(2024.03.06)