CSVクオテーション方式判定
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# CSV クオテーションの有無の判定
#
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
###Wクリックで起動した場合
on run
###ダイアログ
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.comma-separated-values-text"}
set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
### ファイル選択時
set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles, multiple selections allowed and showing package contents) as list
on error
log "エラーしました"
return "エラーしました"
end try
if listAliasFilePath is {} then
return "選んでください"
end if
open listAliasFilePath
end run
on open listAliasFilePath
repeat with itemAliasFilePath in listAliasFilePath
##パス
set aliasFilePath to itemAliasFilePath as alias
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 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
log "文字コードを判定出来ませんでした : " & strFileName
############################
#文字コード指定で読み込む
#ユーザー選択用のレコード
set recordEnc to {|NSISO2022JPStringEncoding|:(refMe's NSISO2022JPStringEncoding), |NSJapaneseEUCStringEncoding|:(refMe's NSJapaneseEUCStringEncoding), |NSShiftJISStringEncoding|:(refMe's NSShiftJISStringEncoding), |NSUTF8StringEncoding|:(refMe's NSUTF8StringEncoding), |NSUTF16LittleEndianStringEncoding|:(refMe's NSUTF16LittleEndianStringEncoding), |NSUTF16StringEncoding|:(refMe's NSUTF16StringEncoding), |NSUnicodeStringEncoding|:(refMe's NSUnicodeStringEncoding), |NSUTF32LittleEndianStringEncoding|:(refMe's NSUTF32LittleEndianStringEncoding), |NSUTF32StringEncoding|:(refMe's NSUTF32StringEncoding), |NSWindowsCP1250StringEncoding|:(refMe's NSWindowsCP1250StringEncoding), |NSWindowsCP1251StringEncoding|:(refMe's NSWindowsCP1251StringEncoding), |NSWindowsCP1252StringEncoding|:(refMe's NSWindowsCP1252StringEncoding), |NSWindowsCP1253StringEncoding|:(refMe's NSWindowsCP1253StringEncoding), |NSWindowsCP1254StringEncoding|:(refMe's NSWindowsCP1254StringEncoding), |NSASCIIStringEncoding|:(refMe's NSASCIIStringEncoding), |NSISOLatin1StringEncoding|:(refMe's NSISOLatin1StringEncoding), |NSISOLatin2StringEncoding|:(refMe's NSISOLatin2StringEncoding), |NSMacOSRomanStringEncoding|:(refMe's NSMacOSRomanStringEncoding), |NSNonLossyASCIIStringEncoding|:(refMe's NSNonLossyASCIIStringEncoding)} as record
#Dictに入れて
set ocidEncDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidEncDict's setDictionary:(recordEnc))
#ダイアログ用にキー名称のみ取り出す
set ocidAllKey to ocidEncDict's allKeys()
#A-z順に並び替え
set ocidSortedAllKey to (ocidAllKey's sortedArrayUsingSelector:("localizedStandardCompare:"))
#リストにしてダイアログに渡す
set listSortedAllKey to ocidSortedAllKey as list
###ダイアログを前面に出す
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 strTitle to ("選んでください") as text
set strPrompt to ("保存エンコードを選んでください\nUTF16-32は通常LEを選んでください") as text
try
set listResponse to (choose from list listSortedAllKey with title strTitle with prompt strPrompt default items (item 5 of listSortedAllKey) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしましたA"
else if (item 1 of listResponse) is {} then
return "キャンセルしましたB"
end if
#戻り値
set strResponse to (item 1 of listResponse) as text
#戻り値をキーにして値=この場合はテキストエンコードを取り出しておく
set ocidSetEnc to (ocidEncDict's objectForKey:(strResponse))
log className() of ocidSetEnc as text
#指定文字コード読み込み
set listResponse to (refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(ocidSetEnc) |error|:(reference))
set ocidReadString to (item 1 of listResponse)
if ocidReadString = (missing value) then
return "指定文字コードで読み込み出来ませんでした : " & strFileName
end if
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))
############################
# 区切り方式判定
#最初の1行目を取得
set ocidItemRowText to (ocidLineArray's firstObject())
#1文字目を取得するためのレンジ
set ocidRange to refMe's NSMakeRange(0, 1)
#1文字目を取得
set strFirstChar to (ocidItemRowText's substringWithRange:(ocidRange)) as text
# カンマ区切り方式の判定
if strFirstChar is "\"" then
log "クオテーション形式のCSV"
set ocidSchar to (refMe's NSString's stringWithString:("\",\""))
else
log "通常のCSV"
set ocidSchar to (refMe's NSString's stringWithString:(","))
end if
############################
# 本処理
############################
#行順に処理
repeat with itemLineArray in ocidLineArray
# 行のテキストをカンマ区切り方式毎にリストに
set ocidRowArray to (itemLineArray's componentsSeparatedByString:(ocidSchar))
# 区切り毎順の処理
repeat with itemRowArray in ocidRowArray
##これが区切り毎のテキスト
log itemRowArray as text
end repeat
end repeat
end repeat
end open
| 固定リンク
« [Acrobat 2020]Acrobat 2020 製品版のアップデータの取得 | トップページ | TSV2AdobeXML タブ区切りテキストからAdobe Illustratorの変数印字用の『変数ライブラリ』を生成する »
「Text CSV」カテゴリの記事
- [CSV2TSV]カンマ区切りテキストをKeyedArchiveしたPlistに変換する (見直し)(2024.12.23)
- [CSV2TSV]カンマ区切りテキストをPlistに変換する (見直し)(2024.12.23)
- [CSV2TSV]カンマ区切りテキストをタブテキストに変換する (見直し)(2024.12.23)
- CSVクオテーション方式判定(2024.03.06)
- CSV2TSV 修正(2024.03.02)