1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
-- -- pgURLAppDelegate.applescript -- pgURL -- -- Created by goodtime on 4/10/11. -- Copyright 2011 NiceMac. All rights reserved. -- demonstrates doing a GET or POST Request using NSURLConnection. Runs in the Background (not in main thread) -- contains no UI... might add one later, for now it just logs or displays alerts -- see console log and feel free to change the URL -- reference material: http://cocoawithlove.com/2008/09/cocoa-application-driven-by-http-data.html script pgURLAppDelegate property parent : class "NSObject" property self : current application's pgURLAppDelegate -- should match the script delegate name above property myURL : "http://www.heise.de" -- URL of your request property postMethod : "POST" -- use POST for POST property myBody : "" -- PUT YOUR POST MESSAGE HERE if trying to do a POST -- URL Request Encoding property myEncoding : NSUTF8StringEncoding of current application --code for UTF8 property getMethod : "GET" -- use GET for any other Requests property intermediateMsg : "" -- Text String returned from the request (may not be complete for long downloads) property entireMsg : "" -- All the Text String returned from the request on pgURL_(myURL, myBody, myMethod) -- String to NSURL tell class "NSURL" of current application set myURL to URLWithString_(myURL) end tell -- String tell class "NSString" of current application set myBody to stringWithString_(myBody) end set myBody to myBody's dataUsingEncoding_(myEncoding) tell class "NSString" of current application set myMethod to myMethod end tell NSMutableURLRequest of current application set myRequest to requestWithURL_(myURL) end tell myRequest setHTTPMethod_(myMethod) end -- add the Message Body when sending a POST Method if myMethod = "POST" then tell myRequest setHTTPBody_(myBody) end end -- form the connection set myConnection to (((current application's class "NSURLConnection")'s alloc)'s initWithRequest_delegate_(myRequest, self)) end -- handle the connection in the background (not in main thread) -- this is controlled by the next four threads on connection_didReceiveResponse_(myConnection, response) tell class "NSHTTPURLResponse" of current application set statusText to (localizedStringForStatusCode_(statusCode of response)) as text set statusCode to (statusCode of response) as string end -- if it fails to do anything, show what it didn't do here (the error) if statustext = "no error" then -- I am not really sure if this does anything or if it is reseting the returnData properly tell current application's returnData setLength_(0) end else display alert "HTTP Error: " & statusCode & return & "Error Message: " & statusText & "." end end on connection_didReceiveData_(myConnection, returnData) -- convert Data returned to String (Don't ever forget how to do this, it is a pain when do forget) set my intermediateMsg to (((current application's class "NSString")'s alloc)'s initWithData_encoding_(returnData, current application's NSUTF8StringEncoding)) as string log "didReceivedData:" log intermediateMsg & return & return set my entireMsg to my entireMsg & intermediateMsg end on connection_didFailWithError_(myConnection,trpErr) -- display alert trpErr as string log trpErr set EM to "" try set newError to (NSLocalizedDescription of userInfo of (trpErr)) as string on error EM -- if AppleScript can't do this, so what else is wrong set errorMore to EM end display alert newError & return & return & errorMore end on connectionDidFinishLoading_(myConnection) log "connection Finished." -- here you can do what you want to do with the intermediateMsg log "here is your entire message returned:" log entireMsg log "end of line." end on applicationWillFinishLaunching_(aNotification) -- Insert code here to initialize your application before any files are opened -- run the request here for demonstration purposes pgURL_(myURL, myBody, getMethod) end applicationWillFinishLaunching_ on applicationShouldTerminate_(sender) -- Insert code here to do any housekeeping before your application quits return current application's NSTerminateNow end applicationShouldTerminate_ end script |