Thursday, August 11, 2022

#1 2022-08-03 09:28:59 pm

akim
Member
Registered: 2010-04-04
Posts: 145

Conversion between open location and Safari URL strings

I am attempting to reconcile a URL string accepted by the command  open location with the URL string returned by Safari's URL string. The check mark character "✓" accepted by the  open location does not match the %E2%9C%93 returned by Safari's URL string.

Applescript:


set TargetURL to "[url]https://go.daisybill.com/queries/patients?utf8=✓[/url]"
tell application "Safari"
   open location TargetURL
   tell current tab of window 1
       set SafariURL to its URL -->"https://go.daisybill.com/queries/patients?utf8=%E2%9C%93"
   end tell
end tell
if SafariURL is TargetURL then
   return true
else
   return false
end if

How can I convert open location's URL to Safari's URL by converting the check mark character "✓" to %E2%9C%93 or vice versa, without using Applescript's text items delimiters to substitute one for the other?

Code posting tags corrected by NG.

Last edited by Nigel Garvey (2022-08-04 09:21:14 am)

Offline

 

#2 2022-08-04 12:51:00 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2443

Re: Conversion between open location and Safari URL strings

akim wrote:

How can I convert open location's URL to Safari's URL by converting the check mark character "✓" to %E2%9C%93 or vice versa, without using Applescript's text items delimiters to substitute one for the other?


This is known as URL encoding <--> decoding. The most effective way I know, is using AsObjC:

Applescript:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

-- vise
set encodedTargetURL to my encodeURLencoding("https://go.daisybill.com/queries/patients?utf8=✓")

-- versa (uncomment following code line to test)
-- set decodedTargetURL to my decodeURLencoding(encodedTargetURL)


on encodeURLencoding(origStr as string)
   set aStr to current application's NSString's stringWithString:origStr
   set encodedStr to aStr's stringByAddingPercentEscapesUsingEncoding:(current application's NSUTF8StringEncoding)
   return encodedStr as string
end encodeURLencoding

on decodeURLencoding(encodedStr)
   set aStr to current application's NSString's stringWithString:encodedStr
   set bStr to aStr's stringByReplacingPercentEscapesUsingEncoding:(current application's NSUTF8StringEncoding)
   return bStr as string
end decodeURLencoding

Last edited by KniazidisR (2022-08-04 12:54:28 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-08-04 10:26:20 am

akim
Member
Registered: 2010-04-04
Posts: 145

Re: Conversion between open location and Safari URL strings

KniazidisR, the simplicity of your script is great!  Thank you so much for your help.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)