URL percent escaping (Studio only)

Note: this only works with Applescript Studio, because applescript “call method” only works here.

This pair of functions (and a diagnostic) will do percentage expansion so “apple itunes” becomes “apple%20itunes”; and the reverse decode out of a percentage escaped url.

  1. Start by opening your Applescript Studio project. Select “New File” from Xcode’s File menu. Use a Cocoa “Objective-C class” file to start. In the next window, give it a name like “ASSURL”, and leave the "Also create “ASSURL.h” header file creation checkbox checked. Technically, in this example, we aren’t using Cocoa - its a CoreFoundation tool.

  2. The default ASSURL.h file will open - this is your header file. Change its non-commented part to:

[code]#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>

@interface NSApplication (ASSURL)

  • (NSString *)setURLFromString:(NSString *)inString;
  • (NSString *)stringFromEncodedURL:(NSString *)urlString;

//- (NSString *) logString:(NSString *)testString;
@end[/code]
Note: the diagnostic function here (logString:) is commented out with “//”. Delete the “//” here and in the *.m file to use it.

  1. Open the ASSURL.m file. Change its non-commented part to:

[code]#import “ASSURL.h”

@implementation NSApplication (ASSURL)

  • (NSString *)setURLFromString:(NSString *)inString
    {
    NSString *linkString = (NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)inString, (CFStringRef)@“:/=”, (CFStringRef)@“!&”, kCFStringEncodingUTF8 );

    NSLog(@“url converted "%@" to "%@"”,inString,linkString);
    return linkString;
    }

  • (NSString *)stringFromEncodedURL:(NSString *)urlString
    {
    NSString *stringString = (NSString *)CFURLCreateStringByReplacingPercentEscapes( NULL, (CFStringRef)urlString, (CFStringRef)@“” );

    NSLog(@“url converted "%@" to "%@"”,urlString,stringString);
    return stringString;
    }

//- (NSString *)logString:(NSString *) testString
//{
// NSLog(@“logging "%@"”, testString); //log it to Console.app - encased in quotes
// return testString; //and return testString
//}

@end[/code]

  1. Save both files. In your Applescript Studio program, call on the functions like so:
on will finish launching theObject
    set someString to "I see London, I see France!"
    set escapedString to call method "setURLFromString:" with parameters {someString}
    log escapedString
end will finish launching
  1. Start your program. Open Console.app, and see:

AAA[5085] url converted “I see London, I see France!” to “I%20see%20London,%20I%20see%20France%21”
AAA[5085] “I%20see%20London,%20I%20see%20France%21”

The first log entry comes from the NSLog, the second from Applescript.

  1. Comment out the NSLog line - it’s just there to show the inner workings.