Alistair Colling
Alistair Colling

Reputation: 1563

How do I escape spaces in a path with Applescript?

I've written a script so I can quickly get a link to files and folders on my server. The problem is that the string I get does not escape spaces. Can someone tell me how to escape the spaces for '%20' ?

tell application "Finder"
set sel to (the POSIX path of (the selection as alias))
set sel to ((characters 10 thru -1 of sel) as string)
set sel to "afp://myserver._afpovertcp._tcp.local/" & sel
set the clipboard to sel      
end tell

Please help thanks!

Upvotes: 1

Views: 2139

Answers (2)

user3577225
user3577225

Reputation:

Following code includes 3 encoding routines from
https://macosxautomation.com/applescript/sbrt/sbrt-08.html

A standard practice when creating URL's is to encode special characters (high-level ASCII) and spaces to their hexidecimal equivalents. For example, spaces in URL's are converted to: %20

Script:

tell application "Finder"
    set sel to (the POSIX path of (the selection as alias))
    set sel to ((characters 10 thru -1 of sel) as string)
    set sel to "afp://myserver._afpovertcp._tcp.local/" & my encode_filepath(sel, true, false)
    set the clipboard to sel
end tell

-- this sub-routine encodes special characters in a filepath:
-- My Disk:My Folder:My File.htm
-- My%20Disk:My%20Folder:My%20File.htm
on encode_filepath(this_file, encode_URL_A, encode_URL_B)
    set this_file to this_file as text
    set AppleScript's text item delimiters to ":"
    set the path_segments to every text item of this_file
    repeat with i from 1 to the count of the path_segments
        set this_segment to item i of the path_segments
        set item i of the path_segments to my encode_text(this_segment, encode_URL_A, encode_URL_B)
    end repeat
    set this_file to the path_segments as string
    set AppleScript's text item delimiters to ""
    return this_file
end encode_filepath

-- TEXT ENCODING: encode spaces and high-level ASCII characters (those above 127)
-- encode_URL_A = encode most of the special characters reserved for use by URLs.
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

-- encoding high-ASCII characters:
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

Upvotes: 0

jackjr300
jackjr300

Reputation: 7191

You can use the text item delimiters to find and replace characters

tell application "Finder" to set sel to POSIX path of (the selection as alias)
set the clipboard to "afp://myserver._afpovertcp._tcp.local/" & (my findReplace(text 10 thru -1 of sel, " ", "%20"))

on findReplace(t, toFind, toReplace)
    set {tid, text item delimiters} to {text item delimiters, toFind}
    set t to text items of t
    set text item delimiters to toReplace
    set t to t as text
    set text item delimiters to tid
    return t
end findReplace

Upvotes: 1

Related Questions