USING: help.markup help.syntax strings ;
-
IN: cocoa.apple-script
+HELP: quote-apple-script
+{ $values { "str" string } }
+{ $description { "Escape special characters in a string to make it suitable as a literal string in AppleScript code." }
+{ $notes "Because this word is a port from Barney Gale's Elevate.py ("{ $vocab-link elevate }"), the only characters escaped are keys in " { $link apple-script-charmap } "; other special characters are unchanged." } ;
+
HELP: run-apple-script
{ $values { "str" string } }
{ $description "Runs the provided uncompiled AppleScript code." }
--- /dev/null
+USING: tools.test ;
+IN: cocoa.apple-script
+
+{ "\\\\" } [ "\\" quote-apple-script ] unit-test
+{ "hello\\nthere" } [ "hello
+there" quote-apple-script ] unit-test ! no space, just a newline
+{ "hello\\rthere" } [ "hello\rthere" quote-apple-script ] unit-test
+{ "hello\\tthere" } [ "hello\tthere" quote-apple-script ] unit-test
+{ "hello\\tthere" } [ "hello there" quote-apple-script ] unit-test ! actual tab character 0x09
+
! Copyright (C) 2013 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
+USING: cocoa cocoa.application cocoa.classes kernel parser
+multiline words ;
+IN: cocoa.apple-script
-USING: cocoa cocoa.application cocoa.classes kernel parser words ;
+<PRIVATE
+CONSTANT: apple-script-charmap H{
+ { "\n" "\\n" }
+ { "\r" "\\r" }
+ { "\t" "\\t" }
+ { "\"" "\\\"" }
+ { "\\" "\\\\" }
+}
+PRIVATE>
-IN: cocoa.apple-script
+: quote-apple-script ( str -- str' )
+ [
+ 1string [ apple-script-charmap at ] [ ] bi or
+ ] { } map-as
+ "" join "\"" dup surround ;
: run-apple-script ( str -- )
[ NSAppleScript -> alloc ] dip
IN: elevate.macosx
<PRIVATE
-CONSTANT: apple-script-charmap H{
- { "\n" "\\n" }
- { "\r" "\\r" }
- { "\t" "\\t" }
- { "\"" "\\\"" }
- { "\\" "\\\\" }
-}
-
-: quote-apple-script ( str -- str' )
- [ 1string [ apple-script-charmap at ] [ ] bi or ] { } map-as
- "" join "\"" dup surround ;
-
: apple-script-elevated ( command -- )
quote-apple-script
"do shell script %s with administrator privileges without altering line endings"