CURL

CURLでのURLチェック (404以外の応答があるURLのみ収集)



ダウンロード - curl_url_checker.zip




AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# URLチェッカー
004# 応答が404以外の場合のURLのみ戻す
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013
014set aliasPathToMe to (path to me) as alias
015set strPathToMe to (POSIX path of aliasPathToMe) as text
016set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe)
017set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath()
018set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false)
019set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent()
020set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("data/URL_LIST.txt") isDirectory:(false)
021#
022set listResponse to refMe's NSString's alloc()'s initWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
023set ocidReadString to (item 1 of listResponse)
024#
025set ocidURLStringArray to ocidReadString's componentsSeparatedByString:("\n")
026#
027set ocidOutPuttring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
028#
029repeat with itemURLstring in ocidURLStringArray
030  if itemURLstring is not "" then
031    set strCommandText to ("/usr/bin/curl -I \"" & itemURLstring & "\"") as text
032    set strResponset to (do shell script strCommandText) as text
033    if strResponset contains "404" then
034      #見つからなかった
035    else
036      log itemURLstring as text
037      (ocidOutPuttring's appendString:(itemURLstring))
038      (ocidOutPuttring's appendString:("\n"))
039    end if
040  end if
041end repeat
042
043##############################
044#####ダイアログ
045##############################
046##前面に出す
047set strName to (name of current application) as text
048if strName is "osascript" then
049  tell application "Finder" to activate
050else
051  tell current application to activate
052end if
053###アイコンパス
054set aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias
055set strMes to ("戻り値です") as text
056set recordResult to (display dialog strMes with title "選んでください" default answer (ocidOutPuttring as text) buttons {"クリップボードにコピー", "キャンセル"} default button "クリップボードにコピー" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
057
058
059###クリップボードコピー
060if button returned of recordResult is "クリップボードにコピー" then
061  set strText to (text returned of recordResult) as text
062  try
063    ####ペーストボード宣言
064    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
065    set ocidText to (refMe's NSString's stringWithString:(strIP))
066    appPasteboard's clearContents()
067    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
068  on error
069    tell application "Finder"
070      set the clipboard to strText as text
071    end tell
072  end try
073end if
074
075
076
077
078
079
080return ocidOutPuttring as text
AppleScriptで生成しました

|

[CURL]ヘッダー付きダウンロード

ブラウザーで右クリックからCURLとしてコピーしてください
AppleScript サンプルコード

【スクリプトエディタで開く】 |

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にしているだけです
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011
012property refMe : a reference to current application
013
014
015###クリックボードの中のURLを取得
016set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
017###クリックボードの中のURLを取得
018set ocidPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)
019#改行除去
020set ocidPasteboardTypeString to (ocidPasteboardTypeString's stringByReplacingOccurrencesOfString:("\r") withString:(""))
021set ocidPasteboardTypeString to (ocidPasteboardTypeString's stringByReplacingOccurrencesOfString:("\n") withString:(""))
022set ocidPasteboardTypeString to (ocidPasteboardTypeString's stringByReplacingOccurrencesOfString:("' \\") withString:("'"))
023set strURL to ocidPasteboardTypeString as text
024
025#元になるCURLとしてコピーしたリンク
026set ocidURLstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
027ocidURLstring's setString:(strURL)
028# ' でリストにする
029set ocidURLstringArray to ocidURLstring's componentsSeparatedByString:("'")
030#リスト数
031set numCntArray to ocidURLstringArray's |count|()
032#HEADER用
033set ocidHeaderString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
034#
035repeat 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
050end repeat
051####
052set strHeader to ocidHeaderString as text
053
054###
055set ocidURLstring to refMe's NSString's stringWithString:(strURL)
056set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstring)
057set strGetURL to ocidURL's absoluteString() as text
058set ocidFileName to ocidURL's lastPathComponent()
059
060###ダウンロードフォルダ
061set appFileManager to refMe's NSFileManager's defaultManager()
062set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
063set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
064#保存ファイルパス
065set ocidSaveFilePathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)
066set strSaveFilePath to ocidSaveFilePathURL's |path| as text
067
068####################
069#実在チェック
070set strCommandText to ("/usr/bin/curl  -I \"" & strGetURL & "\"") as text
071
072log "\r" & strCommandText & "\r"
073
074################## 
075#コマンド実行
076set ocidComString to refMe's NSString's stringWithString:(strCommandText)
077set ocidTermTask to refMe's NSTask's alloc()'s init()
078ocidTermTask's setLaunchPath:("/bin/zsh")
079set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
080ocidArgumentsArray's addObject:("-c")
081ocidArgumentsArray's addObject:(ocidComString)
082ocidTermTask's setArguments:(ocidArgumentsArray)
083set ocidOutPut to refMe's NSPipe's pipe()
084set ocidError to refMe's NSPipe's pipe()
085ocidTermTask's setStandardOutput:(ocidOutPut)
086ocidTermTask's setStandardError:(ocidError)
087ocidTermTask's setCurrentDirectoryURL:(ocidDownloadsDirPathURL)
088set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)
089if (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
094end if
095##################
096#終了待ち
097ocidTermTask's waitUntilExit()
098
099##################
100#標準出力をログに
101set ocidOutPutData to ocidOutPut's fileHandleForReading()
102set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference)
103set ocidStdOut to (item 1 of listResponse)
104set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding)
105##これが戻り値
106set strStdOut to ocidStdOut as text
107if strStdOut contains "404" then
108  return "失敗しました404 見つかりませんでした"
109end if
110
111
112####################
113#ダウンロード本番
114set strCommandText to ("/usr/bin/curl  \"" & strGetURL & "\" -o \"" & strSaveFilePath & "\"  " & strHeader & "") as text
115
116log "\r" & strCommandText & "\r"
117
118################## 
119#コマンド実行
120set ocidComString to refMe's NSString's stringWithString:(strCommandText)
121set ocidTermTask to refMe's NSTask's alloc()'s init()
122ocidTermTask's setLaunchPath:("/bin/zsh")
123set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
124ocidArgumentsArray's addObject:("-c")
125ocidArgumentsArray's addObject:(ocidComString)
126ocidTermTask's setArguments:(ocidArgumentsArray)
127set ocidOutPut to refMe's NSPipe's pipe()
128set ocidError to refMe's NSPipe's pipe()
129ocidTermTask's setStandardOutput:(ocidOutPut)
130ocidTermTask's setStandardError:(ocidError)
131ocidTermTask's setCurrentDirectoryURL:(ocidDownloadsDirPathURL)
132set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)
133if (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
138end if
139##################
140#終了待ち
141ocidTermTask's waitUntilExit()
142
143##################
144#標準出力をログに
145set ocidOutPutData to ocidOutPut's fileHandleForReading()
146set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference)
147set ocidStdOut to (item 1 of listResponse)
148set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding)
149##これが戻り値
150log ocidStdOut as text
151
152
153##################
154#戻り値チェック
155set numReturnNo to ocidTermTask's terminationStatus() as integer
156log numReturnNo
157if numReturnNo = 0 then
158  log "0: 正常終了"
159else
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
174end if
175
176if numReturnNo = 1 then
177  return "1: スクリプトエラー"
178else if numReturnNo = 2 then
179  return "2: 引数エラー"
180else if numReturnNo = 126 then
181  return "126: アクセス権エラー"
182else if numReturnNo = 127 then
183  return "127: スクリプトファイルが見つかりません"
184else if numReturnNo = 130 then
185  return "130: 強制終了"
186else if numReturnNo = 133 then
187  return "133: 異常終了"
188end if
189
AppleScriptで生成しました

|

【CURL】基本的な事項

【基本処理】戻り値
A:データとしてそのまま利用する
B:ファイルにする



【基本処理】処理の基本的な種類
A:GET
B:POST
C:HEAD


【基本処理】戻り値
A:データとしてそのまま利用する
スカイプ バージョンチェッカ
https://quicktimer.cocolog-nifty.com/icefloe/2023/07/post-044a18.html
B:ファイルにする
[Json] plist化して処理する
https://quicktimer.cocolog-nifty.com/icefloe/2022/04/post-7528b6.html


【基本処理】処理の基本的な種類
A:GET
Skype等のアップデートスクリプトで多用しています
https://quicktimer.cocolog-nifty.com/icefloe/cat76054870/index.html

B:POST
[JSON]テキストエディット yahooのAPIを使ってふりがなを付与する
https://quicktimer.cocolog-nifty.com/icefloe/2022/04/post-800313.html
[Json]YahooAPIを使ってテキストにrubyタグを付与
https://quicktimer.cocolog-nifty.com/icefloe/2022/04/post-f1e9f1.html
AppleScript OAuth
https://quicktimer.cocolog-nifty.com/icefloe/cat76050919/index.html
C:HEAD
[CURL]リダイレクト先URL
https://quicktimer.cocolog-nifty.com/icefloe/2023/10/post-5c2a05.html

[CURL]リダイレクト先のファイルサイズ
https://quicktimer.cocolog-nifty.com/icefloe/2023/10/post-3e8a91.html
[CURL]リダイレクト先のファイル名取得
https://quicktimer.cocolog-nifty.com/icefloe/2023/10/post-3daf8a.html

|

[CURL]リダイレクト先URL


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
----+----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


###########【1】リダイレクト先のURLを求める
(*
-I --head ヘッダーのみ
-s サイレント 詳細ログを出さない
-L リダイレクト先の応答を取得
-o /dev/null  出力は捨てる
--write-out -w フォーマットの基づいた結果を表示
*)
set strURL to "https://go.microsoft.com/fwlink/?linkid=2093504" as text
###コマンド整形
set strCommandText to ("/usr/bin/curl --head -s -L -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
##実行
set strRedirectURL to (do shell script strCommandText) as text
##戻り値
log strRedirectURL
return strRedirectURL


|

[CURL]リダイレクト先のファイルサイズ


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
----+----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


###########【1】リダイレクト先のURLを求める
(*
-I --head ヘッダーのみ
-s サイレント 詳細ログを出さない
-L リダイレクト先の応答を取得
-o /dev/null  出力は捨てる
--write-out -w フォーマットの基づいた結果を表示
*)
set strURL to "https://go.microsoft.com/fwlink/?linkid=2093504" as text
###コマンド整形
set strCommandText to ("/usr/bin/curl --head -s -L -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
##実行
set strRedirectURL to (do shell script strCommandText) as text
###########【2】リダイレクト先のファイル名を求める
set strCommandText to ("/usr/bin/basename \"" & strRedirectURL & "\"") as text
set strFileName to (do shell script strCommandText) as text
########【3】リダイレクト先のファイルのファイルサイズを求める
set strCommandText to ("/usr/bin/curl --head -L \"" & strRedirectURL & "\" | grep \"Content-Length\" | /usr/bin/sed 's/[^0-9]*//g'") as text
##実行
set strFileSize to (do shell script strCommandText) as text
##戻り値
log strFileSize
return strFileSize



|

[CURL]リダイレクト先のファイル名取得


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
----+----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


###########【1】リダイレクト先のURLを求める
(*
-I --head ヘッダーのみ
-s サイレント 詳細ログを出さない
-L リダイレクト先の応答を取得
-o /dev/null  出力は捨てる
--write-out -w フォーマットの基づいた結果を表示
*)
set strURL to "https://go.microsoft.com/fwlink/?linkid=2093504" as text
###コマンド整形
set strCommandText to ("/usr/bin/curl --head -s -L -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
##実行
set strRedirectURL to (do shell script strCommandText) as text
###########【2】リダイレクト先のファイル名を求める
set strCommandText to ("/usr/bin/basename \"" & strRedirectURL & "\"") as text
set strFileName to (do shell script strCommandText) as text
log strFileName
return strFileName

|

[CURL]ファイル名を調べて、指定したディレクトリに指定ファイル名でダウンロードする


【スクリプトエディタで開く】|

#!/bin/bash
#com.cocolog-nifty.quicktimer.icefloe

########################################
###管理者インストールしているか?チェック
USER_WHOAMI=$(/usr/bin/whoami)
/bin/echo "実行したユーザーは:$USER_WHOAMI"
if [ "$USER_WHOAMI" != "root" ]; then
  /bin/echo "このスクリプトを実行するには管理者権限が必要です。"
  /bin/echo "sudo で実行してください"
  exit 1
else
  ###実行しているユーザー名
  SUDO_USER=$(/bin/echo "$HOME" | /usr/bin/awk -F'/' '{print $NF}')
  /bin/echo "実行ユーザー:" "$SUDO_USER"
fi

#################################
#インストール基本
#################################

STR_URL="https://go.microsoft.com/fwlink/?linkid=2009112"

LOCAL_TMP_DIR=$(/usr/bin/sudo -u "$SUDO_USER" /usr/bin/mktemp -d )
/bin/echo "TMPDIR:" "$LOCAL_TMP_DIR"

###ファイル名を取得
PKG_FILE_NAME=$(/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' "$STR_URL" | /usr/bin/rev | /usr/bin/cut -d'/' -f1 | /usr/bin/rev )
/bin/echo "PKG_FILE_NAME" "$PKG_FILE_NAME"

###ファイル名指定してダウンロード
/usr/bin/sudo -u "$SUDO_USER"  /usr/bin/curl -L -o "$LOCAL_TMP_DIR/$PKG_FILE_NAME"  "$STR_URL"  --http1.1 --connect-timeout 20

### インストール(上書き)を実行する
/usr/sbin/installer -pkg "$LOCAL_TMP_DIR/$PKG_FILE_NAME" -target / -dumplog -allowUntrusted -lang ja

exit 0


|

[エラー]curl: (18) HTTP/2 stream 1 was not closed cleanly before end of the underlying stream

# curl: (18) HTTP/2 stream 1 was not closed cleanly before end of the underlying stream
# Warning: Got more output options than URLs


【スクリプトエディタで開く】|

#!/bin/bash
###指定無し
/usr/bin/curl -L -o "/path/to/save"  "http://foo.hoge.coom/save"  --connect-timeout 20
## curl: (18) HTTP/2 stream 1 was not closed cleanly before end of the underlying stream
## Warning: Got more output options than URLs
###指定無しでエラーが出る場合はHTTP/1.1を指定してみる
/usr/bin/curl -L -o "/path/to/save"  "http://foo.hoge.coom/save"  --http1.1 --connect-timeout 20
###HTTP/2 指定時
/usr/bin/curl -L -o "/path/to/save"  "http://foo.hoge.coom/save"  --http2 --connect-timeout 20


|

連番ファイル名の連続ダウンロード

【スクリプトエディタで開く】

#!/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 scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

###ヘッダー固定用
set strHeaderText to " -X 'GET' \n-H 'Pragma: no-cache' -H 'Cache-Control: no-cache' \n-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15' \n-H 'Connection: keep-alive'" as text


################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
set ocidPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text
###クリップボードの中身をダイアログのデフォルトに
set strDefaultAnswer to ocidPasteboardStrings as text

################################
######ダイアログ
################################
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias
try
  set recordResponse to (display dialog "詳しく" with title "短め" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
  log "エラーしました"
  return "エラーしました"
  error number -128
end try
if true is equal to (gave up of recordResponse) then
  return "時間切れですやりなおしてください"
  error number -128
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else
  log "エラーしました"
  return "エラーしました"
  error number -128
end if
####################################################
####ヘッダーをストリングにして
set ocidHeaderTextstr to (refMe's NSString's stringWithString:strHeaderText)
###可変テキストに
set ocidHeaderText to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidHeaderText's appendString:ocidHeaderTextstr
###改行除去
set ocidCurlURLLength to ocidHeaderText's |length|()
set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
ocidHeaderText's replaceOccurrencesOfString:("\n") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange
set ocidCurlURLLength to ocidHeaderText's |length|()
set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
ocidHeaderText's replaceOccurrencesOfString:("\r") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange
set strHeaderText to ocidHeaderText as text

####################################################
####URLの処理
set ocidURLstr to (refMe's NSString's stringWithString:strResponse)
set ocidURL to refMe's NSURL's URLWithString:ocidURLstr
set ocidBseURL to ocidURL's URLByDeletingLastPathComponent()
set ocidHostName to ocidURL's |host|()
set ocidExtensionName to ocidURL's pathExtension()
set ocidFileName to ocidBseURL's lastPathComponent()
set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()

####ユーザーダウンロードフォルダ
set ocidUserDownloadsPathArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserDownloadsPathURL to ocidUserDownloadsPathArray's objectAtIndex:0
set ocidSaveDirPathURL to ocidUserDownloadsPathURL's URLByAppendingPathComponent:ocidHostName
###フォルダを作る
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)

set numCntNo to 1 as integer

repeat 50 times
  #####カウンターを追加
  set ocidDownLoadURL to ocidBseURL's URLByAppendingPathComponent:(numCntNo as text)
  set ocidDownLoadURL to ocidDownLoadURL's URLByAppendingPathExtension:ocidExtensionName
  set strDownLoadURL to ocidDownLoadURL's absoluteString() as text
  ###ファイル名確定
  set ocidDownLoadFileName to ocidDownLoadURL's lastPathComponent()
  set ocidDownLoadFileURL to ocidSaveDirPathURL's URLByAppendingPathComponent:ocidDownLoadFileName
  set strDownLoadFileURL to ocidDownLoadFileURL's |path|() as text
  ###コマンド整形 
  set strCommandText to "/usr/bin/curl  '" & strDownLoadURL & "' -o '" & strDownLoadFileURL & "' " & strHeaderText & "" as text
  ###実行
  do shell script strCommandText
  ####カウントアップ
  set numCntNo to numCntNo + 1 as integer
end repeat



|

Curl用のURLコピーからヘッダー分離

【スクリプトエディタで開く】

#!/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 scripting additions

property refMe : a reference to current application

################################
######ペーストボードを取得
################################
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
set ocidPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text


############################
###CURLとしてコピーしているか?
############################
if (ocidPasteboardStrings as text) starts with "curl" then
  set strCurlURL to ocidPasteboardStrings as text
else
  return "CURLとしてコピーしてください"
end if


set strCurlURL to (refMe's NSString's stringWithString:strCurlURL)


####呼び出し
set ocidReturnArray to doSeparateURL(strCurlURL) as list
####結果
set strCopyURL to (item 1 of ocidReturnArray)'s absoluteString() as text
set ocidCopyURL to (item 2 of ocidReturnArray)
set strQuieryText to (item 3 of ocidReturnArray) as text
set strHeaderText to (item 4 of ocidReturnArray) as text


#################################
##### CURL用ヘッダー分離
#################################
to doSeparateURL(argTextCurlURL)
  try
    set refClass to class of argTextCurlURL as text
    if refClass is "text" then
      set ocidCurlURLstr to (refMe's NSString's stringWithString:argTextCurlURL)
    else
      error "テキスト形式以外" number -10000
      return "テキスト形式以外"
    end if
  on error
    set ocidCurlURLstr to argTextCurlURL
  end try
  ####テキスト確定している
  set ocidTextCurlURL to refMe's NSMutableString's alloc()'s initWithCapacity:0
  ocidTextCurlURL's appendString:ocidCurlURLstr
  
  ###戻り値用のリスト
  set ocidDoSeparateURLArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:0
  ###改行を取る
  set ocidCurlURLLength to ocidTextCurlURL's |length|()
  set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
  ocidTextCurlURL's replaceOccurrencesOfString:("\n") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange
  set ocidCurlURLLength to ocidTextCurlURL's |length|()
  set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
  ocidTextCurlURL's replaceOccurrencesOfString:("\r") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange
  
  ###'』でリスト化して2番目がURL
  set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"'"
  set ocidCurlURLArray to ocidTextCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters
  # log ocidCurlURLArray as list
  
  ###URL確定(クエリー入り)
  set ocidURLString to ocidCurlURLArray's objectAtIndex:1
  set ocidURL to refMe's NSURL's URLWithString:ocidURLString
  # log ocidURL's absoluteString() as text
  ocidDoSeparateURLArrayM's addObject:ocidURL
  
  ###URL部分(クエリー無し)
  set ocidURLComponents to (refMe's NSURLComponents)'s componentsWithURL:ocidURL resolvingAgainstBaseURL:true
  ocidURLComponents's setQueryItems:(missing value)
  set ocidBaseURL to ocidURLComponents's |URL|
  # log ocidBaseURL's absoluteString() as text
  ocidDoSeparateURLArrayM's addObject:ocidBaseURL
  
  ###クエリー部分
  set ocidQueryName to ocidURL's query()
  # log ocidQueryName as text
  if ocidQueryName is missing value then
    ocidDoSeparateURLArrayM's addObject:""
  else
    ocidDoSeparateURLArrayM's addObject:ocidQueryName
  end if
  ###ヘッダー部処理
  ###-1はゼロベースなので
  set numCntURLArray to (ocidCurlURLArray's |count|()) - 1 as integer
  set ocidIndexSet to refMe's NSMutableIndexSet's alloc()'s init()
  ###最初がcurlで2つ目がURLだから2コ目から=0ベースなので
  set ocidRange to refMe's NSMakeRange(2, (numCntURLArray - 2))
  set ocidNewIndex to refMe's NSIndexSet's indexSetWithIndexesInRange:ocidRange
  ###必用なインデックスセットで取り出して
  set ocidHeaderArray to (ocidCurlURLArray's objectsAtIndexes:(ocidNewIndex))
  ###テキストにして
  set ocidHeader to ocidHeaderArray's componentsJoinedByString:"'"
  ###最後のシングルクオトが取れちゃうから追加
  ocidHeader's appendString:"'"
  ocidDoSeparateURLArrayM's addObject:ocidHeader
  ###値を戻す
  return ocidDoSeparateURLArrayM
  
end doSeparateURL

|

より以前の記事一覧

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom