[CURL]ヘッダー付きダウンロード
ブラウザーで右クリックからCURLとしてコピーしてください
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | #com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | ##自分環境がos12なので2.8にしているだけです |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AppKit" |
010 | use scripting additions |
011 | |
012 | property refMe : a reference to current application |
013 | |
014 | |
015 | ###クリックボードの中のURLを取得 |
016 | set ocidPasteboard to refMe's NSPasteboard's generalPasteboard() |
017 | ###クリックボードの中のURLを取得 |
018 | set ocidPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString) |
019 | #改行除去 |
020 | set ocidPasteboardTypeString to (ocidPasteboardTypeString's stringByReplacingOccurrencesOfString:("\r") withString:("")) |
021 | set ocidPasteboardTypeString to (ocidPasteboardTypeString's stringByReplacingOccurrencesOfString:("\n") withString:("")) |
022 | set ocidPasteboardTypeString to (ocidPasteboardTypeString's stringByReplacingOccurrencesOfString:("' \\") withString:("'")) |
023 | set strURL to ocidPasteboardTypeString as text |
024 | |
025 | #元になるCURLとしてコピーしたリンク |
026 | set ocidURLstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
027 | ocidURLstring's setString:(strURL) |
028 | # ' でリストにする |
029 | set ocidURLstringArray to ocidURLstring's componentsSeparatedByString:("'") |
030 | #リスト数 |
031 | set numCntArray to ocidURLstringArray's |count|() |
032 | #HEADER用 |
033 | set ocidHeaderString to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
034 | # |
035 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
036 | set strLineString to (ocidURLstringArray's objectAtIndex:(itemNo)) as text |
037 | if strLineString starts with "http" then |
038 | set strURL to strLineString as text |
039 | else if strLineString contains "-H" then |
040 | # |
041 | else if strLineString is "" then |
042 | # |
043 | else if strLineString contains "curl" then |
044 | # |
045 | else |
046 | (ocidHeaderString's appendString:(" -H '")) |
047 | (ocidHeaderString's appendString:(strLineString)) |
048 | (ocidHeaderString's appendString:("'")) |
049 | end if |
050 | end repeat |
051 | #### |
052 | set strHeader to ocidHeaderString as text |
053 | |
054 | ### |
055 | set ocidURLstring to refMe's NSString's stringWithString:(strURL) |
056 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstring) |
057 | set strGetURL to ocidURL's absoluteString() as text |
058 | set ocidFileName to ocidURL's lastPathComponent() |
059 | |
060 | ###ダウンロードフォルダ |
061 | set appFileManager to refMe's NSFileManager's defaultManager() |
062 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)) |
063 | set ocidDownloadsDirPathURL to ocidURLsArray's firstObject() |
064 | #保存ファイルパス |
065 | set ocidSaveFilePathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
066 | set strSaveFilePath to ocidSaveFilePathURL's |path| as text |
067 | |
068 | #################### |
069 | #実在チェック |
070 | set strCommandText to ("/usr/bin/curl -I \"" & strGetURL & "\"") as text |
071 | |
072 | log "\r" & strCommandText & "\r" |
073 | |
074 | ################## |
075 | #コマンド実行 |
076 | set ocidComString to refMe's NSString's stringWithString:(strCommandText) |
077 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
078 | ocidTermTask's setLaunchPath:("/bin/zsh") |
079 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
080 | ocidArgumentsArray's addObject:("-c") |
081 | ocidArgumentsArray's addObject:(ocidComString) |
082 | ocidTermTask's setArguments:(ocidArgumentsArray) |
083 | set ocidOutPut to refMe's NSPipe's pipe() |
084 | set ocidError to refMe's NSPipe's pipe() |
085 | ocidTermTask's setStandardOutput:(ocidOutPut) |
086 | ocidTermTask's setStandardError:(ocidError) |
087 | ocidTermTask's setCurrentDirectoryURL:(ocidDownloadsDirPathURL) |
088 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
089 | if (item 1 of listDoneReturn) is (false) then |
090 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
091 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
092 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
093 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
094 | end if |
095 | ################## |
096 | #終了待ち |
097 | ocidTermTask's waitUntilExit() |
098 | |
099 | ################## |
100 | #標準出力をログに |
101 | set ocidOutPutData to ocidOutPut's fileHandleForReading() |
102 | set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference) |
103 | set ocidStdOut to (item 1 of listResponse) |
104 | set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding) |
105 | ##これが戻り値 |
106 | set strStdOut to ocidStdOut as text |
107 | if strStdOut contains "404" then |
108 | return "失敗しました404 見つかりませんでした" |
109 | end if |
110 | |
111 | |
112 | #################### |
113 | #ダウンロード本番 |
114 | set strCommandText to ("/usr/bin/curl \"" & strGetURL & "\" -o \"" & strSaveFilePath & "\" " & strHeader & "") as text |
115 | |
116 | log "\r" & strCommandText & "\r" |
117 | |
118 | ################## |
119 | #コマンド実行 |
120 | set ocidComString to refMe's NSString's stringWithString:(strCommandText) |
121 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
122 | ocidTermTask's setLaunchPath:("/bin/zsh") |
123 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
124 | ocidArgumentsArray's addObject:("-c") |
125 | ocidArgumentsArray's addObject:(ocidComString) |
126 | ocidTermTask's setArguments:(ocidArgumentsArray) |
127 | set ocidOutPut to refMe's NSPipe's pipe() |
128 | set ocidError to refMe's NSPipe's pipe() |
129 | ocidTermTask's setStandardOutput:(ocidOutPut) |
130 | ocidTermTask's setStandardError:(ocidError) |
131 | ocidTermTask's setCurrentDirectoryURL:(ocidDownloadsDirPathURL) |
132 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
133 | if (item 1 of listDoneReturn) is (false) then |
134 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
135 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
136 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
137 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
138 | end if |
139 | ################## |
140 | #終了待ち |
141 | ocidTermTask's waitUntilExit() |
142 | |
143 | ################## |
144 | #標準出力をログに |
145 | set ocidOutPutData to ocidOutPut's fileHandleForReading() |
146 | set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference) |
147 | set ocidStdOut to (item 1 of listResponse) |
148 | set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding) |
149 | ##これが戻り値 |
150 | log ocidStdOut as text |
151 | |
152 | |
153 | ################## |
154 | #戻り値チェック |
155 | set numReturnNo to ocidTermTask's terminationStatus() as integer |
156 | log numReturnNo |
157 | if numReturnNo = 0 then |
158 | log "0: 正常終了" |
159 | else |
160 | ################## |
161 | #エラーをログに |
162 | set ocidErrorData to ocidError's fileHandleForReading() |
163 | set listResponse to ocidErrorData's readDataToEndOfFileAndReturnError:(reference) |
164 | set ocidErrorOutData to (item 1 of listResponse) |
165 | set ocidErrorOutString to refMe's NSString's alloc()'s initWithData:(ocidErrorOutData) encoding:(refMe's NSUTF8StringEncoding) |
166 | set listResponse to refMe's NSFileHandle's fileHandleForWritingToURL:(ocidLogFilePathURL) |error| :(reference) |
167 | set ocidReadHandle to (item 1 of listResponse) |
168 | set listDone to ocidReadHandle's seekToEndReturningOffset:(0) |error| :(reference) |
169 | log (item 1 of listDone) as boolean |
170 | set listDone to ocidReadHandle's writeData:(ocidErrorOutData) |error| :(reference) |
171 | log (item 1 of listDone) as boolean |
172 | set listDone to ocidReadHandle's closeAndReturnError:(reference) |
173 | log (item 1 of listDone) as boolean |
174 | end if |
175 | |
176 | if numReturnNo = 1 then |
177 | return "1: スクリプトエラー" |
178 | else if numReturnNo = 2 then |
179 | return "2: 引数エラー" |
180 | else if numReturnNo = 126 then |
181 | return "126: アクセス権エラー" |
182 | else if numReturnNo = 127 then |
183 | return "127: スクリプトファイルが見つかりません" |
184 | else if numReturnNo = 130 then |
185 | return "130: 強制終了" |
186 | else if numReturnNo = 133 then |
187 | return "133: 異常終了" |
188 | end if |
189 | |
AppleScriptで生成しました |
| 固定リンク
「CURL」カテゴリの記事
- [curl]rapid-cloudビデオ・ダウンロードヘルパー(簡易式)(2025.01.16)
- CURLでのURLチェック (404以外の応答があるURLのみ収集)(2024.12.03)
- [CURL]ヘッダー付きダウンロード(2024.11.23)
- 【CURL】基本的な事項(2023.10.24)
- [CURL]リダイレクト先URL(2023.10.19)