001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | (* |
---|
004 | com.cocolog-nifty.quicktimer.icefloe |
---|
005 | *) |
---|
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
007 | use AppleScript version "2.8" |
---|
008 | use framework "Foundation" |
---|
009 | use framework "AppKit" |
---|
010 | use scripting additions |
---|
011 | property refMe : a reference to current application |
---|
012 | |
---|
013 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
014 | |
---|
015 | ###ダイアログを前面に出す |
---|
016 | set strName to (name of current application) as text |
---|
017 | if strName is "osascript" then |
---|
018 | tell application "Finder" to activate |
---|
019 | else |
---|
020 | tell current application to activate |
---|
021 | end if |
---|
022 | #デフォルトロケーション |
---|
023 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
024 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
025 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
---|
026 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
---|
027 | # |
---|
028 | set listUTI to {"public.xml"} |
---|
029 | set strMes to ("ファイルを選んでください") as text |
---|
030 | set strPrompt to ("ファイルを選んでください") as text |
---|
031 | try |
---|
032 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
---|
033 | on error |
---|
034 | log "エラーしました" |
---|
035 | return "エラーしました" |
---|
036 | end try |
---|
037 | ###入力ファイル |
---|
038 | set strFilePath to (POSIX path of aliasFilePath) as text |
---|
039 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
---|
040 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
---|
041 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
---|
042 | |
---|
043 | ### XML読み込み |
---|
044 | set ocidOption to (refMe's NSXMLDocumentTidyXML) |
---|
045 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
---|
046 | if (item 2 of listResponse) ≠ (missing value) then |
---|
047 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
048 | return "読み込みに失敗しました" |
---|
049 | else if (item 2 of listResponse) = (missing value) then |
---|
050 | set ocidReadXMLDoc to (item 1 of listResponse) |
---|
051 | end if |
---|
052 | |
---|
053 | #最終的に保存するテキスト |
---|
054 | set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
---|
055 | ocidSaveString's setString:("タイトル\tID\n") |
---|
056 | #値取得用のDICT |
---|
057 | set ocidSaveDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
058 | |
---|
059 | ###ROOT |
---|
060 | set ocidRootElement to ocidReadXMLDoc's rootElement() |
---|
061 | #bookmark-tree |
---|
062 | set ocidBkmTreeArray to ocidRootElement's elementsForName:("bookmark-tree") |
---|
063 | set ocidBkmTree to ocidBkmTreeArray's firstObject() |
---|
064 | set ocidBkmArray to ocidBkmTree's elementsForName:("bookmark") |
---|
065 | set numCntBkmRoot to (count of ocidBkmArray) as integer |
---|
066 | #ルート要素の数だけ繰り返し |
---|
067 | repeat with itemNo from 0 to (numCntBkmRoot - 1) by 1 |
---|
068 | #取り出して |
---|
069 | set ocidBkmItem to (ocidBkmArray's objectAtIndex:(itemNo)) |
---|
070 | #値を取得して |
---|
071 | set ocidTitleValue to (ocidBkmItem's attributeForName:("title"))'s stringValue() |
---|
072 | set ocidDistArray to (ocidBkmItem's elementsForName:("destination")) |
---|
073 | set ocidDist to ocidDistArray's firstObject() |
---|
074 | set ocidDistValue to (ocidDist's attributeForName:("structID"))'s stringValue() |
---|
075 | #出力用に保存していく |
---|
076 | (ocidSaveString's appendString:("▼")) |
---|
077 | (ocidSaveString's appendString:(ocidTitleValue)) |
---|
078 | (ocidSaveString's appendString:("\t")) |
---|
079 | (ocidSaveString's appendString:(ocidDistValue)) |
---|
080 | (ocidSaveString's appendString:("\n")) |
---|
081 | (ocidSaveDict's setValue:(ocidDistValue) forKey:(ocidTitleValue)) |
---|
082 | #子要素があるか?調べて |
---|
083 | set ocidBkmSubArray to (ocidBkmItem's elementsForName:("bookmark")) |
---|
084 | set numCntCild to (count of ocidBkmSubArray) as integer |
---|
085 | #子要素があれば子要素取得に回す |
---|
086 | if numCntCild = 0 then |
---|
087 | log "子要素無" |
---|
088 | else |
---|
089 | log "子要素有" |
---|
090 | set listResponse to doGetBkmValue({ocidBkmSubArray, ocidSaveString, ocidSaveDict}) |
---|
091 | log class of listResponse |
---|
092 | set ocidSaveString to (item 1 of listResponse) |
---|
093 | set ocidSaveDict to (item 2 of listResponse) |
---|
094 | end if |
---|
095 | end repeat |
---|
096 | |
---|
097 | ############ |
---|
098 | ###子要素取得 |
---|
099 | to doGetBkmValue({argBkmSubArray, argSaveString, argSaveDict}) |
---|
100 | #子要素の数を数えて |
---|
101 | set numCntCild to (count of argBkmSubArray) as integer |
---|
102 | #要素の数だけ繰り返し |
---|
103 | repeat with itemChildNo from 0 to (numCntCild - 1) by 1 |
---|
104 | #値を取得して |
---|
105 | set ocidBkmItem to (argBkmSubArray's objectAtIndex:(itemChildNo)) |
---|
106 | set ocidTitleValue to (ocidBkmItem's attributeForName:("title"))'s stringValue() |
---|
107 | set ocidDistArray to (ocidBkmItem's elementsForName:("destination")) |
---|
108 | set ocidDist to ocidDistArray's firstObject() |
---|
109 | set ocidDistValue to (ocidDist's attributeForName:("structID"))'s stringValue() |
---|
110 | #保存用データに渡す |
---|
111 | (argSaveString's appendString:(ocidTitleValue)) |
---|
112 | (argSaveString's appendString:("\t")) |
---|
113 | (argSaveString's appendString:(ocidDistValue)) |
---|
114 | (argSaveString's appendString:("\n")) |
---|
115 | (argSaveDict's setValue:(ocidDistValue) forKey:(ocidTitleValue)) |
---|
116 | #子要素があるか確認して |
---|
117 | set ocidBkmSubArray to (ocidBkmItem's elementsForName:("bookmark")) |
---|
118 | set numCntCild to (count of ocidBkmSubArray) as integer |
---|
119 | #子要素がなければ親要素に戻る |
---|
120 | if numCntCild = 0 then |
---|
121 | return ({argSaveString, argSaveDict}) |
---|
122 | else |
---|
123 | log "Childあり" |
---|
124 | ##子要素があれば子要素取得する |
---|
125 | doGetBkmValue({ocidBkmSubArray, argSaveString, argSaveDict}) |
---|
126 | end if |
---|
127 | end repeat |
---|
128 | end doGetBkmValue |
---|
129 | |
---|
130 | ############################## |
---|
131 | #保存先URL |
---|
132 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
---|
133 | set ocidTsvFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("bkm.tsv") |
---|
134 | |
---|
135 | #テキスト保存 |
---|
136 | set listDone to ocidSaveString's writeToFile:(ocidTsvFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
---|
137 | if (item 1 of listDone) is true then |
---|
138 | log "正常終了" |
---|
139 | else if (item 1 of listDone) is false then |
---|
140 | log (item 2 of listDone)'s localizedDescription() as text |
---|
141 | return "保存に失敗しました" |
---|
142 | end if |
---|
143 | |
---|
144 | return |
---|
145 | |
---|
146 | #PLIST保存 必要があれば |
---|
147 | set ocidPlistFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("plist") |
---|
148 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
---|
149 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidSaveDict) format:(ocidFormat) options:0 |error| :(reference) |
---|
150 | if (item 2 of listResponse) ≠ (missing value) then |
---|
151 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
152 | return "データ変換に失敗しました" |
---|
153 | else if (item 2 of listResponse) = (missing value) then |
---|
154 | set ocidPlistData to (item 1 of listResponse) |
---|
155 | end if |
---|
156 | set ocidOption to (refMe's NSDataWritingAtomic) |
---|
157 | set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error| :(reference) |
---|
158 | if (item 2 of listResponse) ≠ (missing value) then |
---|
159 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
160 | return "保存に失敗しました" |
---|
161 | else if (item 2 of listResponse) = (missing value) then |
---|
162 | return "正常終了" |
---|
163 | end if |
---|
164 | |
---|
165 | ###開く |
---|
166 | set aliasFilePath to (ocidTsvFilePathURL's absoluteURL()) as alias |
---|
167 | tell application "Finder" |
---|
168 | open location aliasFilePath |
---|
169 | end tell |
---|