AppleScript CURL

【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

|

dizigomビデオダウンローダー(少しなおした)

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
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()

## 1時間 320程度
set numMaxRepeat to 10000 as number
set numSaveFileNo to 10000 as number


###クリックボードの中のURLを取得
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
###クリックボードの中のURLを取得
set ocidPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)
set ocidCurlURL to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidCurlURL's setString:ocidPasteboardTypeString
##可変テキスト形式で格納
set ocidCurlURL to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidCurlURL's setString:ocidPasteboardTypeString

############################
###CURLとしてコピーしているか?
############################
if (ocidCurlURL as text) starts with "curl" then
  log "処理開始"
  #############################
  ###URL を分解
  #############################
  set ocidDoSeparateURLArrayM to doSeparateURL(ocidCurlURL)
  set strM3uURL to ocidCurlURL as text
else
  return "CURLとしてコピーしてください"
end if
#############################
###分解したURLの各種値
#############################
set strCopyURL to (item 1 of ocidDoSeparateURLArrayM)'s absoluteString() as text
set ocidCopyURL to (item 2 of ocidDoSeparateURLArrayM)
set ocidQuiery to (item 3 of ocidDoSeparateURLArrayM) as text
set strHeaderText to (item 4 of ocidDoSeparateURLArrayM) as text
#####ファイル名
set ocidContainerCopyURL to ocidCopyURL's URLByDeletingLastPathComponent()
set strBaseURL to ocidContainerCopyURL's absoluteString() as text
#####保存パス用の日付番号
set ocidDate to refMe's NSDate's |date|()
set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setDateFormat:"yyyyMMddhhmmss"
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
#####保存先のファイルパス
set strTempPath to ("/private/tmp/" & (ocidDateAndTime as text) & "") as text
#####NSStringファイルパス
set ocidDirPath to (refMe's NSString's stringWithString:strTempPath)
#####FileManager
######保存用のフォルダを作る
set listBoolMakeDir to appFileManager's createDirectoryAtPath:(ocidDirPath) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
set boolMakeDir to (item 1 of listBoolMakeDir) as boolean
if boolMakeDir is false then
  error "フォルダ作成に失敗しました" number -2753 from "フォルダ作成に失敗しました"
  return
end if
#####################################
#####繰り返し
#####################################
###ファイル番号用のカウンタ
set numCnt to 0 as number
####最大繰り返し数を一応設定
repeat numMaxRepeat times
  ###最初にカウントアップ
  set numCnt to numCnt + 1 as number
  set numSaveFileNo to numSaveFileNo + 1 as number
  ####10以下はゼロサプレスあり
  if numCnt < 10 then
    set strCnt to numCnt as text
    set strCnt to "0" & strCnt as text
    set strFileName to ("gomplayer-" & strCnt & ".jpeg") as text
  else
    set strFileName to ("gomplayer-" & numCnt & ".jpeg") as text
  end if
  ####ファイル番号
  set strURL to (strBaseURL & strFileName) as text
  set strTsFileName to (numSaveFileNo & ".ts")
  set strTsDownPath to (strTempPath & "/" & strTsFileName) as text
  ####コマンド実行(ヘッダー付き)
  set strCommandText to "/usr/bin/curl  '" & strURL & "' -o '" & strTsDownPath & "' " & strHeaderText & "" as text
  do shell script strCommandText
  ####ファイルサイズを調べて
  set ocidDownFilePath to refMe's NSString's stringWithString:strTsDownPath
  set ocidTsFilePath to ocidDownFilePath's stringByStandardizingPath
  set ocidAttributes to appFileManager's attributesOfItemAtPath:ocidTsFilePath |error|:(missing value)
  set numFileSize to ocidAttributes's NSFileSize as integer
  ####1KB以下なら終了
  if numFileSize < 1024 then
    exit repeat
  end if
  
end repeat

do shell script "cat  " & strTempPath & "/* >> ~/Desktop/" & (ocidDateAndTime as text) & ".ts"




to doSeparateURL(ocidCurlURL)
  ###戻り値用のリスト
  set ocidDoSeparateURLArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:0
  
  ###改行を取る
  set ocidCurlURLLength to ocidCurlURL's |length|()
  set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
  ocidCurlURL's replaceOccurrencesOfString:("\n") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange
  ###'』でリスト化して2番目がURL
  set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"'"
  set ocidCurlURLArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters
  
  ###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
  ###ヘッダー部処理
  set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"\\"
  set ocidHeaderArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters
  set ocidHeaderArrayLength to ocidHeaderArray's |count|()
  set ocidHeaderRange to refMe's NSMakeRange(1, (ocidHeaderArrayLength - 1))
  set ocidSubbArray to ocidHeaderArray's subarrayWithRange:ocidHeaderRange
  set ocidHeaderText to (ocidSubbArray's componentsJoinedByString:"")
  
  log ocidHeaderText as text
  ocidDoSeparateURLArrayM's addObject:ocidHeaderText
  ###値を戻す
  return ocidDoSeparateURLArrayM
  
end doSeparateURL

|

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

ダウンロード - curl.scpt.zip


#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos13なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application

property refNSString : a reference to refMe's NSString

property refNSURL : a reference to refMe's NSURL

property refNSData : a reference to refMe's NSData

property refNSMutableString : a reference to refMe's NSMutableString

property refNSMutableArray : a reference to refMe's NSMutableArray


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



property refNSPasteboard : a reference to refMe's NSPasteboard

set ocidPasteboard to refNSPasteboard's generalPasteboard()


###クリックボードの中のURLを取得

set ocidPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)


##可変テキスト形式で格納

set ocidCurlURL to refNSMutableString's alloc()'s initWithCapacity:0

ocidCurlURL's setString:ocidPasteboardTypeString


if (ocidCurlURL as text) starts with "curl" then

    log "処理開始"

    set ocidDoSeparateURLArrayM to doSeparateURL(ocidCurlURL)

else

    return "CURLとしてコピーしてください"

end if


set strURLwithQuery to (item 1 of ocidDoSeparateURLArrayM)'s absoluteString() as text

set strURL to (item 2 of ocidDoSeparateURLArrayM)'s absoluteString() as text

set strQuery to (item 3 of ocidDoSeparateURLArrayM) as text

set strHeader to (item 4 of ocidDoSeparateURLArrayM) as text


set ocidURL to (item 2 of ocidDoSeparateURLArrayM)

set ocidFileName to ocidURL's lastPathComponent()


set ocidUserDownloadsPath to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))

set ocidDownLoadFolderPath to item 1 of ocidUserDownloadsPath

set ocidFilePathURL to ocidDownLoadFolderPath's URLByAppendingPathComponent:ocidFileName isDirectory:false

set strFilePath to ocidFilePathURL's |path|() as text

log strFilePath


set strCommandText to "/usr/bin/curl  '" & strURL & "' -o '" & strFilePath & "' " & strHeader & "" as text

do shell script strCommandText


to doSeparateURL(ocidCurlURL)

    ###戻り値用のリスト

    set ocidDoSeparateURLArrayM to refNSMutableArray's alloc()'s initWithCapacity:0

    

    ###改行を取る

    set ocidCurlURLLength to ocidCurlURL's |length|()

    set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}

    ocidCurlURL's replaceOccurrencesOfString:("\n") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange

    ###'』でリスト化して2番目がURL

    set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"'"

    set ocidCurlURLArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters

    

    ###URL確定(クエリー入り)

    set ocidURLString to ocidCurlURLArray's objectAtIndex:1

    set ocidURL to refNSURL'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

    ###ヘッダー部処理

    set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"\\"

    set ocidHeaderArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters

    set ocidHeaderArrayLength to ocidHeaderArray's |count|()

    set ocidHeaderRange to refMe's NSMakeRange(1, (ocidHeaderArrayLength - 1))

    set ocidSubbArray to ocidHeaderArray's subarrayWithRange:ocidHeaderRange

    set ocidHeaderText to (ocidSubbArray's componentsJoinedByString:"")

    

    #log ocidHeaderText as text

    ocidDoSeparateURLArrayM's addObject:ocidHeaderText

    ###値を戻す

    return ocidDoSeparateURLArrayM

    

end doSeparateURL

|

その他のカテゴリー

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