Sunday, December 4, 2022

#1 2022-11-10 02:53:50 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 30

Change spaces in theResult url?

Hi,

I'm trying to extend a script I'm using. It now breaks the url when there's a space in the filepath.

Whats I now get is:
https://download.url.com/folder1/best practises

Whats I need is:
https://download.url.com/folder1/best%20practises

Applescript:

return expand("")

on ReplaceText(theString, fString, rString)
   set current_Delimiters to text item delimiters of AppleScript
   set AppleScript's text item delimiters to fString
   set sList to every text item of theString
   set AppleScript's text item delimiters to rString
   set newString to sList as string
   set AppleScript's text item delimiters to current_Delimiters
   return newString
end ReplaceText

on expand(separator)
   
   tell application "Finder"
       set theSelection to get selection as list
       set theResult to ""
       if length of separator = 0 then set separator to linefeed
       repeat with theItem in theSelection
           if length of theResult > 0 then set theResult to theResult & separator
           set theResult to theResult & POSIX path of (theItem as alias)
           set theResult to ReplaceText(theResult, "/Volumes/DISK_A", "https://download.url.com") of me
       end repeat
       return theResult
   end tell
   
end expand

Offline

 

#2 2022-11-10 02:55:02 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 30

Re: Change spaces in theResult url?

I found a script here, but it is not clear how to add this to the script above and if this is the right one?

Applescript:

replaceChars("/Volumes/A Hard Drive", space, "%20")

on replaceChars(aString, charToReplace, newChar)
set {TID, text item delimiters} to {text item delimiters, charToReplace}
set aString to text items of aString
set text item delimiters to newChar
set aString to aString as text
set text item delimiters to TID
return aString
end replaceChars

Anybody here how to implement this?

Thanks.

Offline

 

#3 2022-11-10 07:24:57 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1518

Re: Change spaces in theResult url?

vanwoods wrote:

I'm trying to extend a script I'm using. It now breaks the url when there's a space in the filepath.

Whats I now get is:
https://download.url.com/folder1/best practises

Whats I need is:
https://download.url.com/folder1/best%20practises



Vanwoods. The ReplaceText and replaceChars handlers are functionally the same, and both work for me. Just as a matter of personal preference, I would use the replaceChars handler. However, I don't understand their use in the expand handler. Instead of line 1 below shouldn't it be line 2 below. I don't understand the purpose of the expand handler, so perhaps there's some reason for this?

Applescript:

set theResult to ReplaceText(theResult, "/Volumes/DISK_A", "https://download.url.com") of me
set theResult to ReplaceText(theResult, space, "%20") of me

Last edited by peavine (2022-11-10 08:07:05 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2022-11-11 03:27:44 am

vanwoods
Member
Registered: 2021-03-28
Posts: 30

Re: Change spaces in theResult url?

Hi peavine,

Thanks for getting back to me!

It's in an expand handler because I use it for text expansion in email. We are selecting the file in the finder and then expand the script when typing a text string. It gives a download link which can be send to the client.

The below script works now but I don't know if this is the best setup. To copy the line multiple times?

Applescript:

return expand("")

on ReplaceText(theString, fString, rString)
set current_Delimiters to text item delimiters of AppleScript
set AppleScript's text item delimiters to fString
set sList to every text item of theString
set AppleScript's text item delimiters to rString
set newString to sList as string
set AppleScript's text item delimiters to current_Delimiters
return newString
end ReplaceText

on expand(separator)

tell application "Finder"
set theSelection to get selection as list
set theResult to ""
if length of separator = 0 then set separator to linefeed
repeat with theItem in theSelection
if length of theResult > 0 then set theResult to theResult & separator
set theResult to theResult & POSIX path of (theItem as alias)
set theResult to ReplaceText(theResult, "/Volumes/DISK_A", "https://download.url.com") of me
set theResult to ReplaceText(theResult, space, "%20") of me
set theResult to ReplaceText(theResult, "/Volumes/DISK_B", "https://download.url.com") of me
set theResult to ReplaceText(theResult, space, "%20") of me
set theResult to ReplaceText(theResult, "/Volumes/DISK_C", "https://download.url.com") of me
set theResult to ReplaceText(theResult, space, "%20") of me
end repeat
return theResult
end tell

end expand

Offline

 

#5 2022-11-11 05:04:04 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Change spaces in theResult url?

You could do it with ASObjC, with your examples...

Applescript:

use framework "Foundation"
use scripting additions

set URLString to "download.url.com/folder1/best practises"
set theURL to current application's |NSURL|'s fileURLWithPath:URLString
return "https://" & (theURL's resourceSpecifier())

Last edited by Fredrik71 (2022-11-11 09:34:01 am)


Node-RED makes it easy to automate IoT

Offline

 

#6 2022-11-12 02:03:34 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 30

Re: Change spaces in theResult url?

Fredrik71 wrote:

You could do it with ASObjC, with your examples...

Applescript:

use framework "Foundation"
use scripting additions

set URLString to "download.url.com/folder1/best practises"
set theURL to current application's |NSURL|'s fileURLWithPath:URLString
return "https://" & (theURL's resourceSpecifier())



Thanks for getting back. I tried to combine the above script with my version but I couldn't get it to work. Is this approach better than what I have?

A s in the previous post it's now working as what I needed, just wondered if this was the best way?

Thanks for your help!!

Last edited by vanwoods (2022-11-12 02:04:47 pm)

Offline

 

#7 2022-11-12 03:23:20 pm

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: Change spaces in theResult url?

Here is a slightly cleaned up version
I moved most of the routine 'expand' outside of the tell "Finder" block.

Applescript:

return expand("")

on ReplaceText(theString, fString, rString)
   local tid, newString
   set tid to text item delimiters
   set text item delimiters to fString
   set sList to every text item of theString
   set text item delimiters to rString
   set newString to sList as string
   set text item delimiters to tid
   return newString
end ReplaceText

on expand(separator)
   local tid, theSelection, theResult
   set tid to text item delimiters
   tell application "Finder" to set theSelection to selection
   if length of separator = 0 then set separator to linefeed
   repeat with theItem in theSelection
       set contents of theItem to POSIX path of (theItem as alias)
   end repeat
   set text item delimiters to linefeed
   -- uncomment end of line below to have trailing linefeed
   set theResult to (theSelection as text) -- & linefeed
   set theResult to ReplaceText(theResult, "/Volumes/DISK_A", "https://download.url.com")
   set theResult to ReplaceText(theResult, "/Volumes/DISK_B", "https://download.url.com")
   set theResult to ReplaceText(theResult, "/Volumes/DISK_C", "https://download.url.com")
   set theResult to ReplaceText(theResult, space, "%20") -- only needs to be done once
   set text item delimiters to tid
   return theResult
end expand

Last edited by robertfern (2022-11-12 05:07:19 pm)

Offline

 

#8 2022-11-12 04:50:08 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Change spaces in theResult url?

When I need to open URL from web view on my local computer I use fileURLWithPath method
and absoluteString(). In other words if the path to the file has space it will use %20 in the URL address.

In your case you also like to have https:// instead of file:/// and thats why I used resourceSpecifier() and append https:// at begining.

If you like AppleScript to open a local URL you could use absoluteString() instead.


Node-RED makes it easy to automate IoT

Offline

 

#9 2022-11-12 05:11:14 pm

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: Change spaces in theResult url?

And here is another one with using URL property of selection.
It makes it so we don't have to replace spaces with %20

Applescript:

return expand("")

on ReplaceText(theString, fString, rString)
   local tid, newString
   set tid to text item delimiters
   set text item delimiters to fString
   set sList to every text item of theString
   set text item delimiters to rString
   set newString to sList as string
   set text item delimiters to tid
   return newString
end ReplaceText

on expand(separator)
   local tid, theSelection, theResult
   set tid to text item delimiters
   if length of separator = 0 then set separator to linefeed
   tell application "Finder"
       set theSelection to selection
       repeat with theItem in theSelection
           set contents of theItem to URL of theItem
       end repeat
   end tell
   set text item delimiters to linefeed
   -- uncomment end of line below to have trailing linefeed
   set theResult to (theSelection as text) -- & linefeed
   set theResult to ReplaceText(theResult, "file:///Volumes/", "https://download.url.com/")
   set text item delimiters to tid
   return theResult
end expand

Last edited by robertfern (2022-11-12 05:14:25 pm)

Offline

 

#10 2022-11-12 05:14:20 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Change spaces in theResult url?

You could also do it with NSString.

The first script remove %20 and replace it with space, the second do the opposite.

Applescript:

use framework "Foundation"

set URLString to "https://download.url.com/folder1/best%20practises"
set theString to (current application's NSString's stringWithString:URLString)'s ¬
   stringByRemovingPercentEncoding() as string


set URLString to "https://download.url.com/folder1/best practises"
set theString to ((current application's NSString's stringWithString:URLString)'s ¬
   stringByAddingPercentEncodingWithAllowedCharacters:(current application's NSCharacterSet's URLPathAllowedCharacterSet())) as string


Node-RED makes it easy to automate IoT

Offline

 

#11 2022-11-13 03:02:06 am

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

Re: Change spaces in theResult url?

The process of replacing special characters in a URL string is called URL encoding. In my experience, AsObjC  and ruby handle this most successfully and consistently. The correct AsObjC-handler provided by @Fredrik71 above.

I prefer the ruby solution in cases where AsObjC is not needed by the rest of the script. Because bootstrapping the AsObjC frameworks kills. Note: I commented taking the finder selection and hardcoded the selected file in my example. You can use commented code line instead.

Applescript:


-- tell application "Finder" to set posixPath to POSIX path of ((item 1 of (get selection)) as text)
set posixPath to "/Volumes/DISK_A/folder1/best practises"

set newPath to ReplaceText(posixPath, "/Volumes/DISK_A/", "https://download.url.com/")
set theURL to encodeURL_ruby(newPath)


on ReplaceText(theString, fString, rString)
   set current_Delimiters to text item delimiters of AppleScript
   set AppleScript's text item delimiters to fString
   set sList to every text item of theString
   set AppleScript's text item delimiters to rString
   set newString to sList as string
   set AppleScript's text item delimiters to current_Delimiters
   return newString
end ReplaceText


on encodeURL_ruby(theURL)
   do shell script "ruby -e 'require \"uri\";puts URI.escape(\"" & theURL & "\")'"
end encodeURL_ruby

If you do not take into account the bootstrap time of frameworks, then the AsObjC solution is the fastest of all. The second fastest is the perl solution.

I did not include it from the very beginning only because it also encodes the characters ":" and "/", and as I understand it, the OP do not want this. Although it is this URL encoding that is the standard.

Applescript:


set posixPath to "/Volumes/DISK_A/folder1/best practises"

set newPath to ReplaceText(posixPath, "/Volumes/DISK_A/", "https://download.url.com/")
set theURL to encodeURL_perl(newPath)


on ReplaceText(theString, fString, rString)
   set current_Delimiters to text item delimiters of AppleScript
   set AppleScript's text item delimiters to fString
   set sList to every text item of theString
   set AppleScript's text item delimiters to rString
   set newString to sList as string
   set AppleScript's text item delimiters to current_Delimiters
   return newString
end ReplaceText


on encodeURL_perl(theURL) -- the fastest Plain AppleScript method
   do shell script "/usr/bin/perl -e 'use URI::Escape; print uri_escape(\"" & theURL & "\")';"
end encodeURL_perl

Last edited by KniazidisR (2022-11-13 04:27:20 am)


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

Offline

 

#12 2022-11-13 04:48:21 am

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

Re: Change spaces in theResult url?

Here is very nice AppleScript-solution which doesn't use shell commands and AsObjC frameworks at all. It is very very fast and allows user defined character sets to encode (see standard_characters, URL_A_chars and URL_A_chars lists in the handler).

You can see the the symbol ":" in URL_B_chars and symbol "/" in URL_A_chars. So, I put to not encode both lists when calling the handler (parameters false, false). 

Applescript:


set posixPath to "/Volumes/DISK_A/folder1/best practises"
set newPath to ReplaceText(posixPath, "/Volumes/DISK_A/", "https://download.url.com/")

encode_text(newPath, false, false)

on ReplaceText(theString, fString, rString)
   set current_Delimiters to text item delimiters of AppleScript
   set AppleScript's text item delimiters to fString
   set sList to every text item of theString
   set AppleScript's text item delimiters to rString
   set newString to sList as string
   set AppleScript's text item delimiters to current_Delimiters
   return newString
end ReplaceText

on encode_text(this_text, encode_URL_A, encode_URL_B)
   set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
   set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
   set the URL_B_chars to ".-_:"
   set the acceptable_characters to the standard_characters
   if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
   if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
   set the encoded_text to ""
   repeat with this_char in this_text
       if this_char is in the acceptable_characters then
           set the encoded_text to (the encoded_text & this_char)
       else
           set the encoded_text to (the encoded_text & encode_char(this_char)) as string
       end if
   end repeat
   return the encoded_text
end encode_text

on encode_char(this_char)
   set the ASCII_num to (the ASCII number this_char)
   set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
   set x to item ((ASCII_num div 16) + 1) of the hex_list
   set y to item ((ASCII_num mod 16) + 1) of the hex_list
   return ("%" & x & y) as string
end encode_char

Last edited by KniazidisR (2022-11-13 05:07:40 am)


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

Offline

 

#13 2022-11-13 05:55:00 am

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

Re: Change spaces in theResult url?

Finally, I should note that the original problem can be solved even more efficiently. To do this, you need to use the fact that the Finder itself provides the URL-encoded form "file:///". You just need to stupidly replace "file:///" with "https://".    wink

Applescript:


set URLs to {}
tell application "Finder"
   repeat with anItem in (get selection)
       set end of URLs to my replaceText(URL of anItem, "file:///", "https://")
   end repeat
end tell
return URLs

on replaceText(theString, fString, rString)
   set ATID to text item delimiters of AppleScript
   set AppleScript's text item delimiters to fString
   set sList to text items of theString
   set AppleScript's text item delimiters to rString
   set newString to sList as text
   set AppleScript's text item delimiters to ATID
   return newString
end replaceText

Last edited by KniazidisR (2022-11-13 06:10:09 am)


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

Offline

 

#14 2022-11-14 05:01:39 pm

vanwoods
Member
Registered: 2021-03-28
Posts: 30

Re: Change spaces in theResult url?

Thanks, I tried al the above scripts and they work like a charm!

Only one more question, is it possible to have to the second part after /VOLUMES/DISK_A or DISK_B or DISK_C be replaced no matter how the disk (or share) is called? Copying the lines like below is resulting in normal posixPaths and not the url (https://download.url.com/)

Applescript:

set newPath to ReplaceText(posixPath, "/Volumes/DISK_A/", "[url]https://download.url.com/[/url]")
set newPath to ReplaceText(posixPath, "/Volumes/DISK_B/", "[url]https://download.url.com/[/url]")
set newPath to ReplaceText(posixPath, "/Volumes/DISK_C/", "[url]https://download.url.com/[/url]")

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)