START > APPLESCRIPT > Praktische Handler > Text URI-Encoding

Text URI-Encoding

Mit diesem AppleScript-Handler lassen sich Texte für URLs (URI-Encoding) codieren. Die Sonderzeichen eines übergebenen Strings werden hiermit URL-tauglich umgewandelt.

encode_text("Dies ist ein 100%iger Text")
-- Ergebnis: "Dies%20ist%20ein%20100%25iger%20Text"
encode_char("%")
-- Ergebnis: "%25"

-- :::::::::::::::::::::::::::::::::  hohabadu.de  :::::::::::::::::::::::::::::::::

-- this sub-routine is used to encode text (adapted from an Apple example)
on encode_text(this_text)
   set the acceptable_characters to "abcdefghijklmnopqrstuvwxyz0123456789_/.-"
   set the encoded_text to ""
   set the character_list to {}
   repeat with this_char in this_text
      set this_char to the contents of this_char
      if this_char is in the acceptable_characters then
         set the end of the character_list to this_char
      else
         set the end of the character_list to encode_char(this_char)
      end if
   end repeat
   return (the character_list) as string
end encode_text

-- this sub-routine is used to encode a character (unchanged from Apple original example)
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

 Im Scripteditor öffnen



Powered By CMSimple Design By NMuD Top