SuperCard Shell Essentials

 

Path Conversions

Page history last edited by DCS 3 yrs ago

Path Conversions

osascript

Example 1

on mouseup
--Synopsis: convert project folder Mac path to Unix path via 'osascript'
--               set current working directory with 'cd' command
--               list contents of project folder with 'ls -F'
put "`" into bq
--command expansion operator
put projectpath(this proj) into tgtfolder
--project folder Mac path
put "cd `[[bq]]osascript -e 'posix path of alias `[[tgtfolder]]`' [[bq]]` ; " into cmd
--Change working directory to project folder
--The [[bq]] placeholders are replaced by a backquote after merge inserts quotes
--leaving the 'osascript' syntax enclosed in double quotes after merge.
--The command expansion metacharacters make nested quotes legal.
--If the Mac path heirarchy includes a double quote, this method will fail.
--Bash allows an alternate command expansion method...
--put "cd `$(osascript -e 'posix path of alias `[[tgtfolder]]`')` ; " into cmd
put "ls -F ; " after cmd
--list contents of project folder
put merge(cmd) into cmd
--merge command syntax
get shell(cmd)
--execute commands 
ask list it prompt "Project folder contents" return text
--display result
end mouseup

Top

 

Example 2

The example below uses AppleScript OSA for Mac to Unix path conversion.

The backquote character (unshifted tilde) used in the SuperCard merge function for quote replacement, is also part of shell command substitution syntax. Which explains the '[[bq]]' merge placeholder.

Commands used: osascript, perl, echo, iconv, eval, file.

Iconv ships with OSX Panther and newer, but works with earlier versions. Google libiconv.

 

on mouseup
--Synopsis; ask user to select a file
--               convert Mac path to unix path via 'osascript'
--               get information about the file using 'file -b' and 'file -bi'.
answer file "Select a file"
if it = "" then return ""
put it into macpath
put "`" into bq
--the unshifted tilde (backquote) is at command expansion metacharacter
--the enlosed command is executed and the result served up as a value
put merge("_path=[[bq]]osascript -e 'posix path of alias `[[macpath]]`' | perl -pe 's/ /\ /g' [[bq]] ; ") into cmd
--set the variable '_path' is set to the result of osascript/perl commands
--the osascript command passes the utf8 encoded unix path to perl
--perl replaces spaces with an escape sequence
--the first backslash escapes the 2nd
--the last, escapes <space> itselfi
--oddly, 4-6 backslashes work just well
put "echo -n 'path:' ; " after cmd
--adds a label for following command
put "echo $_path | iconv -f utf8-mac -t macroman | perl -pe 's/\ / /g' ;" after cmd
--pass the _path var to the icon v command for conversion to macroman encoding
--conversion from utf8 is at issue only if the ascii value of a path char is > 127
--the $ symbol before a variable label indicates scalar rather than array variable
--the _path variable remains utf8 encoded
put "echo '' ; " after cmd
--adds a blank line
put "echo 'Info:' `eval file -b $_path` ; " after cmd
--echo prints the char string 'Info:" followed by
--the result of the eval/file command (note the enclosing the backquotes), 
--and a trailing linefeed
--the eval command returns the file command 
--eval allows the path to be resolved from the variable _path
--the file command returns some info about the file
--similar to above...
put "echo -n 'Mime Type:' `eval file -bi $_path`" after cmd
--then -n switch following echo prevents the trailing linefeed
--the -bi switch following 'file'... 
--'-b' prevents path from printing and
--'-i' tell file to return a mime type file descriptor
--execute and display result in alert sheet
alert sheet "file info" explain shell(cmd)
end mouseup

Top

Comments (0)

You don't have permission to comment on this page.