NSURLComponents

[NSURLComponents]URLにクエリーの追加(考え中)

そのままでは追加できないので
一度削除して追加しなおす方法を採ったが
何か他にいい方法があるような気がする
AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "UniformTypeIdentifiers"
010use framework "AppKit"
011use scripting additions
012property refMe : a reference to current application
013
014
015
016set strURL to "https://some.domain/search?keyword=検索語句" as text
017#NSURLにして
018set ocidURLString to refMe's NSString's stringWithString:(strURL)
019set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
020#クエリーアイテムに分解しておいて
021set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false)
022#現在のクエリーアイテムを取得しておく
023set ocidGetQueryArray to ocidURLomponents's queryItems()
024-->SwiftDeferredNSArrayなのでそのままでは追加出来ない
025#空のクエリーをセットすることでクエリーを除去しておく
026ocidURLomponents's setQuery:("")
027#新規にクエリーアイテムを作成する
028set ocidQueryItems to refMe's NSMutableArray's alloc()'s init()
029set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("someQUERY") value:("someVALUE")
030ocidQueryItems's addObject:(ocidQueryItem)
031#取り出しておいたクエリをセットし直す
032repeat with itemGetAuery in ocidGetQueryArray
033  (ocidQueryItems's addObject:(itemGetAuery))
034end repeat
035ocidURLomponents's setQueryItems:(ocidQueryItems)
036##URLに戻して テキストにしておく
037set ocidOpenURL to ocidURLomponents's |URL|()
038set strOpenURL to ocidOpenURL's absoluteString() as text
039
040log strOpenURL
041set strURL to ocidURL's absoluteString() as text
042set ocidHostName to ocidURL's |host|() as text
043set ocidFileName to ocidURL's lastPathComponent()
044set ocidExtension to ocidURL's pathExtension()
AppleScriptで生成しました

|

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

### 【1】RSSのURL
#基本となるURL
set strURL to ("https://mac.softpedia.com/backend.xml") as text
#NSURLにして
set ocidBaseURL to refMe's NSURL's alloc()'s initWithString:(strURL)
#コンポーネントに分割
set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidBaseURL) resolvingAgainstBaseURL:(false)
#クエリーアイテム
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
#クエリー定義
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("") value:("_all")
#クエリーをセット
ocidQueryItems's addObject:(ocidQueryItem)
#コンポーネントに追加
ocidURLomponents's setQueryItems:(ocidQueryItems)
#URLを再定義
set ocidURL to ocidURLomponents's |URL|
#ホスト
set strHost to ocidURL's |host|() as text

|

[FaceTime]電話をかける


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

#!/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 framework "AppKit"
use scripting additions
property refMe : a reference to current application


########################
## クリップボードの中身取り出し
########################
##クリップボードにテキストが無い場合
set strMes to ("電話番号を入力してください") as text
###初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to ocidPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
  ###値を格納する
  tell application "Finder"
    set strDefaultAnswer to (the clipboard as text) as text
  end tell
  ###Finderでエラーしたら
else
  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
  if boolContain = true then
    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set strDefaultAnswer to ocidReadString as text
  else
    log "テキストなし"
    set strDefaultAnswer to strMes as text
  end if
end if
##############################
#####ダイアログ
##############################
##前面に出す
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 aliasIconPath to POSIX file "/System/Applications/Calculator.app/Contents/Resources/AppIcon.icns" as alias
try
  set recordResult to (display dialog strMes with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 30 without hidden answer) as record
on error
  log "エラーしました"
return "キャンセル"
end try

if "OK" is equal to (button returned of recordResult) then
  set strReturnedText to (text returned of recordResult) as text
else if (gave up of recordResult) is true then
return "時間切れです"
else
return "キャンセル"
end if

##############################
#####本処理
##############################
###NSStringに格納
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
##改行除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
##半角
set ocidNSStringTransform to (refMe's NSStringTransformFullwidthToHalfwidth)
set ocidTextM to (ocidTextM's stringByApplyingTransform:(ocidNSStringTransform) |reverse|:false)
##ハイフン除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("-") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("−") withString:("")
###########################
###URL整形
set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
###スキーム を追加
ocidURLComponents's setScheme:("tel")
###パスを追加(setHostじゃないよ)
ocidURLComponents's setPath:(ocidTextM)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidURLComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text
###########################
### OPEN URL
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
set boolDone to appShardWorkspace's openURL:(ocidOpenURL)

return

tell application id "com.apple.FaceTime"
  open location strOpenURL
end tell
(*
##別なアプリを指定している場合
tell application "Finder"
open location strOpenURL
end tell
*)


|

[NSURLComponents] まとめ

【1】http
[NSURLComponents] http系の基本処理見本
https://quicktimer.cocolog-nifty.com/icefloe/2023/11/post-1601f5.html

【2】mailto
[NSURL] eMailのNSURL
https://quicktimer.cocolog-nifty.com/icefloe/2023/06/post-992871.html
[NSURLComponents]mailto:のURLの扱いについて
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-b15d5f.html

【3】他
x-apple-findertag
[x-apple-findertag]タグのNSURL
https://quicktimer.cocolog-nifty.com/icefloe/2023/06/post-271bd5.html

macappstore
[AppStore]アップデートページを開く
https://quicktimer.cocolog-nifty.com/icefloe/2023/10/post-a25cd6.html

x-apple.systempreferences
システム設定をオープンさせるスクリプトの作成補助v3
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-af2f58.html

edge
https://quicktimer.cocolog-nifty.com/icefloe/2023/08/post-cca752.html

chrome
https://quicktimer.cocolog-nifty.com/icefloe/2023/08/post-55cc93.html

【基本】
[NSURLComponents] initWithString
https://quicktimer.cocolog-nifty.com/icefloe/2022/09/post-1f5451.html
[queryItems]クエリーの各アイテムの値を取得
https://quicktimer.cocolog-nifty.com/icefloe/2023/02/post-e4218d.html

|

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 framework "AppKit"
use scripting additions
property refMe : a reference to current application


###元になるURL
set strURL to ("https://hoge.domain.com/some/dir/some/file.exe?q=someQuery")
set ocidURL to refMe's NSURL's URLWithString:(strURL)
log ocidURL's absoluteString() as text
(*https://hoge.domain.com/some/dir/some/file.exe?q=someQuery*)

##クエリー部取り出し
set ocidQuery to ocidURL's query()
log ocidQuery as text
(*q=someQuery*)

##NSURLComponents クエリーを削除 クエリーがなくなるので?がない
set ocidURLComponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false)
##クエリーにNULL=クエリー削除=missing value
ocidURLComponents's setQuery:(missing value)
set ocidURL to ocidURLComponents's |URL|()
log ocidURL's absoluteString() as text
(*https://hoge.domain.com/some/dir/some/file.exe*)

##NSURLComponents 空のクエリーをセット ?が残る
set ocidURLComponents to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false)
##クエリーにNULL=クエリー削除
ocidURLComponents's setQuery:("")
set ocidURL to ocidURLComponents's |URL|()
log ocidURL's absoluteString() as text
(*https://hoge.domain.com/some/dir/some/file.exe?*)


|

[NSURLComponents] http系の基本処理見本


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

#!/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 framework "AppKit"
use scripting additions
property refMe : a reference to current application


###########################
###URL整形
set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
###スキーム を追加
ocidURLComponents's setScheme:("https")
###ホスト追加
ocidURLComponents's setHost:("acrobat.adobe.com")
###パスを追加(setHostじゃないよ)
ocidURLComponents's setPath:("/link/file")
##クエリー部
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
##ファイルタイプ クエリー1
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("filetype") value:("application/pdf")
ocidQueryItems's addObject:(ocidQueryItem)
##アセットID クエリー2
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("uri") value:("someID")
ocidQueryItems's addObject:(ocidQueryItem)
##クエリーをセットする
ocidURLComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidURLComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text

|

[twitter]twitterで自分の投稿を検索する


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

#!/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 framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
########################
###設定項目
########################
###自分のID
set strMyTwiiterID to ("floe_ice") as text
###開くアプリケーション
set strBundleID to "com.microsoft.edgemac" as text
(* ここは好みで変更
com.apple.Safari
com.google.Chrome
org.mozilla.firefox
*)
########################
## クリップボードの中身取り出し
########################
###初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
set ocidPastBoardTypeArray to ocidPasteboard's types
###テキストがあれば
set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
if boolContain = true then
  ###値を格納する
  tell application "Finder"
    set strReadString to (the clipboard as text) as text
  end tell
  ###Finderでエラーしたら
else
  set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
  if boolContain = true then
    set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
    set strReadString to ocidReadString as text
  else
    log "テキストなし"
    set strReadString to "検索文字列を入力" as text
  end if
end if
##############################
#####ダイアログ
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
set aliasIconPath to (POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MagnifyingGlassIcon.icns") as alias
try
  
  set strMes to "検索文字列を入力してください\nユーザー指定:@" & strMyTwiiterID & "でtwitter検索します" as text
  
  set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 10 without hidden answer) as record
on error
  log "エラーしました"
return
end try

if "OK" is equal to (button returned of recordResult) then
  set strReturnedText to (text returned of recordResult) as text
else if (gave up of recordResult) is true then
return "時間切れです"
else
return "キャンセル"
end if


################################
## 本処理
################################
set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
###タブと改行を除去しておく
set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
##改行除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
##タブ除去
set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
set strReturnedText to ocidTextM as text
##検索文字列にする
set strSearchString to ("@" & strMyTwiiterID & " " & strReturnedText) as text
##@を%エンコードしたいならこちら
## set strSearchString to ("%40" & strMyTwiiterID & " " & strReturnedText) as text
set ocidViewURLString to refMe's NSString's stringWithString:(strSearchString)
##クエリーアイテムにしておく
set ocidQueryItemQ to refMe's NSURLQueryItem's alloc()'s initWithName:("q") value:(ocidViewURLString)
##クエリーアイテム『ズ』でセットするのArrayに入れておく
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidQueryItems's addObject:(ocidQueryItemQ)
##TOPの場合
#set ocidQueryItemF to refMe's NSURLQueryItem's alloc()'s initWithName:("f") value:("top")
##Latestの場合
set ocidQueryItemF to refMe's NSURLQueryItem's alloc()'s initWithName:("f") value:("live")
ocidQueryItems's addObject:(ocidQueryItemF)
##
set ocidQueryItemSRC to refMe's NSURLQueryItem's alloc()'s initWithName:("src") value:("typed_query")
ocidQueryItems's addObject:(ocidQueryItemSRC)


##############
###送信するURL
set strURL to ("https://twitter.com/search") as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###コンポーネントに分解
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
##クエリーをセットする
ocidComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text

################################
## OPEN
################################
tell application id strBundleID
  open location strOpenURL
end tell

####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20", |/|:"%2F"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode
####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode

|

[pagespeed.web.dev]前面ページをpagespeed.web.devに送る

Safari

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

#!/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 framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application


set strBundleID to "com.apple.Safari" as text


###全面のタブのURLを取得して
tell application "Safari"
  set numCntWindow to (count of every window) as integer
  if numCntWindow = 0 then
return "ウィンドウがありません"
  end if
end tell
tell application "Safari"
  set numID to id of front window
  set objTab to current tab of window id numID
  
  tell window id numID
    tell objTab
      set strViewURL to URL as text
    end tell
  end tell
end tell
##############
###
if strViewURL is (missing value) then
return "ページを開いていません"
end if

################################
## 本処理
################################
###表示中のURLを%エンコード-->不要でした
##set strEncURL to doTextEncode(strViewURL)
set ocidViewURLString to refMe's NSString's stringWithString:(strViewURL)
##クエリーアイテムにしておく
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("url") value:(ocidViewURLString)
##クエリーアイテム『ズ』でセットするのArrayに入れておく
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidQueryItems's addObject:(ocidQueryItem)
##############
###送信するURL
set strURL to ("https://pagespeed.web.dev/analysis") as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###コンポーネントに分解
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
##クエリーをセットする
ocidComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text

################################
## OPEN
################################
tell application id strBundleID
  open location strOpenURL
end tell

####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20", |/|:"%2F"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode
####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode

com.google.Chrome

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

#!/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 framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application


set strBundleID to "com.google.Chrome" as text


###全面のタブのURLを取得して
tell application "Google Chrome"
  tell front window
    tell active tab
      set strViewURL to URL as text
    end tell
  end tell
end tell
##############
###
if strViewURL is (missing value) then
return "ページを開いていません"
end if

################################
## 本処理
################################
###表示中のURLを%エンコード-->不要でした
##set strEncURL to doTextEncode(strViewURL)
set ocidViewURLString to refMe's NSString's stringWithString:(strViewURL)
##クエリーアイテムにしておく
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("url") value:(ocidViewURLString)
##クエリーアイテム『ズ』でセットするのArrayに入れておく
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidQueryItems's addObject:(ocidQueryItem)
##############
###送信するURL
set strURL to ("https://pagespeed.web.dev/analysis") as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###コンポーネントに分解
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
##クエリーをセットする
ocidComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text

################################
## OPEN
################################
tell application id strBundleID
  open location strOpenURL
end tell

####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20", |/|:"%2F"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode
####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode

org.mozilla.firefox

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

#!/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 framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application


set strBundleID to "org.mozilla.firefox" as text


###全面のタブのURLを取得して
####ページ情報を開く
tell application "Firefox"
  tell front window
    activate
    tell application "System Events"
      tell process "Firefox"
keystroke "i" using {command down}
      end tell
    end tell
  end tell
end tell

delay 1


#####ページ情報の名称を取得
tell application "Firefox"
  tell window 1
    set strPageName to name as text
  end tell
end tell


####ページ情報を閉じる
tell application "Firefox"
  tell window 1
close
  end tell
end tell



set strViewURL to doReplace(strPageName, "ページ情報 — ", "")
##############
###
if strViewURL is (missing value) then
return "ページを開いていません"
end if

################################
## 本処理
################################
###表示中のURLを%エンコード-->不要でした
##set strEncURL to doTextEncode(strViewURL)
set ocidViewURLString to refMe's NSString's stringWithString:(strViewURL)
##クエリーアイテムにしておく
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("url") value:(ocidViewURLString)
##クエリーアイテム『ズ』でセットするのArrayに入れておく
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidQueryItems's addObject:(ocidQueryItem)
##############
###送信するURL
set strURL to ("https://pagespeed.web.dev/analysis") as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###コンポーネントに分解
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
##クエリーをセットする
ocidComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text

################################
## OPEN
################################
tell application id strBundleID
  open location strOpenURL
end tell

####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20", |/|:"%2F"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode
####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode

####################################
###### 文字の置換
####################################
to doReplace(theText, orgStr, newStr)
  set oldDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to orgStr
  set tmpList to every text item of theText
  set AppleScript's text item delimiters to newStr
  set tmpStr to tmpList as text
  set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace


com.microsoft.edgemac

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

#!/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 framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application


set strBundleID to "com.microsoft.edgemac" as text


###全面のタブのURLを取得して
tell application "Microsoft Edge"
  tell front window
    tell active tab
      set strViewURL to URL as text
    end tell
  end tell
end tell
##############
###
if strViewURL is (missing value) then
return "ページを開いていません"
end if

################################
## 本処理
################################
###表示中のURLを%エンコード-->不要でした
##set strEncURL to doTextEncode(strViewURL)
set ocidViewURLString to refMe's NSString's stringWithString:(strViewURL)
##クエリーアイテムにしておく
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("url") value:(ocidViewURLString)
##クエリーアイテム『ズ』でセットするのArrayに入れておく
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidQueryItems's addObject:(ocidQueryItem)
##############
###送信するURL
set strURL to ("https://pagespeed.web.dev/analysis") as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###コンポーネントに分解
set ocidComponents to refMe's NSURLComponents's alloc()'s initWithURL:(ocidURL) resolvingAgainstBaseURL:(true)
##クエリーをセットする
ocidComponents's setQueryItems:(ocidQueryItems)
##URLに戻して テキストにしておく
set ocidOpenURL to ocidComponents's |URL|()
set strOpenURL to ocidOpenURL's absoluteString() as text

################################
## OPEN
################################
tell application id strBundleID
  open location strOpenURL
end tell

####################################
###### %エンコード
####################################
on doUrlEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ##テキスト形式に確定
  set strTextToEncode to ocidArgTextEncoded as text
  ###値を戻す
return strTextToEncode
end doUrlEncode
####################################
###### %エンコード
####################################
on doTextEncode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##キャラクタセットを指定
  set ocidChrSet to refMe's NSCharacterSet's URLQueryAllowedCharacterSet
  ##キャラクタセットで変換
  set ocidArgTextEncoded to ocidArgText's stringByAddingPercentEncodingWithAllowedCharacters:(ocidChrSet)
  ######## 置換 %エンコードの追加処理
  ###置換レコード
  set recordPercentMap to {|!|:"%21", |#|:"%23", |$|:"%24", |&|:"%26", |'|:"%27", |(|:"%28", |)|:"%29", |*|:"%2A", |+|:"%2B", |,|:"%2C", |:|:"%3A", |;|:"%3B", |=|:"%3D", |?|:"%3F", |@|:"%40", | |:"%20", |/|:"%2F"} as record
  ###ディクショナリにして
  set ocidPercentMap to refMe's NSDictionary's alloc()'s initWithDictionary:(recordPercentMap)
  ###キーの一覧を取り出します
  set ocidAllKeys to ocidPercentMap's allKeys()
  ###取り出したキー一覧を順番に処理
  repeat with itemAllKey in ocidAllKeys
    set strItemKey to itemAllKey as text
    ##キーの値を取り出して
    if strItemKey is "@" then
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:("@") withString:("%40"))
    else
      set ocidMapValue to (ocidPercentMap's valueForKey:(strItemKey))
      ##置換
      set ocidEncodedText to (ocidArgTextEncoded's stringByReplacingOccurrencesOfString:(strItemKey) withString:(ocidMapValue))
    end if
    ##次の変換に備える
    set ocidArgTextEncoded to ocidEncodedText
  end repeat
  ##テキスト形式に確定
  set strTextToEncode to ocidEncodedText as text
  ###値を戻す
return strTextToEncode
end doTextEncode
####################################
###### %デコード
####################################
on doUrlDecode(argText)
  ##テキスト
  set ocidArgText to refMe's NSString's stringWithString:(argText)
  ##デコード
  set ocidArgTextEncoded to ocidArgText's stringByRemovingPercentEncoding
  set strArgTextEncoded to ocidArgTextEncoded as text
return strArgTextEncoded
end doUrlDecode

|

[NSURLComponents]mailto:のURLの扱いについて(Emailアドレスを取り出すにはコンポーネントに分割してから)


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

#!/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 strURL to "mailto:foo@hoge.com?subject=Hi" as text
set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL)

log ocidURL's absoluteString() as text
-->(*mailto:foo@hoge.com?subject=Hi*)
log ocidURL's |scheme|() as text
-->(*mailto*)
log ocidURL's |path|() as text
-->(*missing value*)
log ocidURL's query() as text
-->(*missing value*)
###Emailアドレスを取り出すにはコンポーネントに分割してから
set ocidComponent to refMe's NSURLComponents's componentsWithURL:(ocidURL) resolvingAgainstBaseURL:(false)
log ocidComponent's |path|() as text
-->(*foo@hoge.com*)
log ocidComponent's query() as text
-->(*subject=Hi*)



|

[x-apple-findertag]タグのNSURL


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

#!/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 framework "AppKit"
use scripting additions

property refMe : a reference to current application

set itemTagName to "レッド"
###コンポーネント初期化
set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
###スキーム を追加
ocidURLComponents's setScheme:("x-apple-findertag")
###パスを追加(setHostじゃないよ)
ocidURLComponents's setPath:(itemTagName)
set ocidTagURL to ocidURLComponents's |URL|
log ocidTagURL's absoluteString() as text
-->(*x-apple-findertag:%E3%83%AC%E3%83%83%E3%83%89*)

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings 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 Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom