Re: [OT] AppleScript scripts versus Shell scripts
Re: [OT] AppleScript scripts versus Shell scripts
- Subject: Re: [OT] AppleScript scripts versus Shell scripts
- From: Philip Aker <email@hidden>
- Date: Tue, 28 Jan 2003 13:14:33 -0800
On Tuesday, January 28, 2003, at 05:49 AM, Nigel Smith wrote:
But no-one's even mentioned Tcl yet -- probably the UNIX scripting
system most analogous to AppleScript...
Nigel!
I know what you mean by Tcl and AppleScript being kindred spirits. Now
that MacOS X comes with a Tcl component, I have recently found out that
the Tcl team will be more than happy to provide core features making it
easier to use from AppleScript in the next release. This will include a
built-in shell script enabling direct calls to Tcl such are currently
used with -c or -e options in other scripting languages. The shell
script currently under consideration is just below. It may be called
"tclexec" in the future. I've undertaken to provide a few examples for
calling from AppleScript and they are just below as well. They show how
to use search & replace in strings, get a dictionary sorted list, and
convert to hexadecimal values.
If anyone has any comments, suggestions, or wish-list items for Tcl
implementation, please forward them to me and I'll make sure they get
echoed to the Tcl team.
-- Shell script to invoke tclsh directly. Must mark as executable and
place in your search path.
-- Schnipp here. AppleScripts below.
#!/usr/bin/tclsh
if {$argc < 1 } {
set me [file tail $argv0]
puts stderr "Error: '$me' call has parameter(s) missing\nUsage: $me
\"commands\""
exit 1
}
set result 0
foreach call $argv {
set result [catch {eval $call} errortext]
if {$result} {
puts stderr $errortext
} else {
puts stdout $errortext
}
}
exit $result
-- APPLESCRIPTS (put in separate files)
-- "-->" is the output
--#1
-- Tcl proc to obtain the hexadecimal values in a Tcl integer list
-- Uses the 'puts' command with option '-nonewline'
set Tcl_GetHex to "proc Tcl_GetHex {args} {
foreach arg $args {
puts -nonewline [format \"%X \" $arg];
}
}
"
-- Get the hexadecimal string representation of each number in a list
of integers
-- returns a list
on GetHexValues(theNumList)
set res to {}
set AppleScript's text item delimiters to " "
set strlist to theNumList as string
set AppleScript's text item delimiters to ""
set cmd1 to quoted form of (my Tcl_GetHex & "Tcl_GetHex " & strlist)
--set res1 to (do shell script "tcl " & cmd1)
set res1 to words of (do shell script "tcl " & cmd1) as list
end GetHexValues
set numlist to {100, 2000, 30000, 400000, 5000000, 600, 700, 800, 900}
GetHexValues(numlist)
--> {"64", "7D0", "7530", "61A80", "4C4B40", "258", "2BC", "320", "384"}
--#2
-- Tcl proc to obtain the hexadecimal values in a Tcl integer list
-- Uses the efficient 'append' command and trims the trailing space
-- Each hex value gets a "0x" prefix, uses uppercase hex numbers
-- Use [format \"0x%x\" $arg] for lowercase hex
set Tcl_GetHexWithPrefix to "proc Tcl_GetHex {args} {
set str \"\";
foreach arg $args {
append str [format \"0x%X \" $arg];
}
return [string trimright $str];
}
"
-- Get the hexadecimal string representation of each number in a list
of integers
-- returns a string
on GetHexValuesWithPrefix(theNumList)
set AppleScript's text item delimiters to " "
set strlist to theNumList as string
set AppleScript's text item delimiters to ""
set cmd1 to quoted form of (my Tcl_GetHexWithPrefix & "Tcl_GetHex " &
strlist)
set res1 to (do shell script "tcl " & cmd1)
end GetHexValuesWithPrefix
set numlist to {100, 2000, 30000, 400000, 5000000, 600, 700, 800, 900}
set hexlist to GetHexValuesWithPrefix(numlist)
--> "0x64 0x7D0 0x7530 0x61A80 0x4C4B40 0x258 0x2BC 0x320 0x384"
--#3
-- How to use Tcl's 'regsub' search and replace ignoring case
set Tcl_regsub to quoted form of "proc Tcl_regsub {theSearchPattern
theString {theReplacePattern \"\"}} {
regsub -all -nocase -- $theSearchPattern $theString
$theReplacePattern res;
puts -nonewline $res;
}
"
set cmd1 to quoted form of "info commands"
set res1 to (do shell script "tcl " & cmd1)
set cmd2 to quoted form of ("lsort -dict [list " & res1 & "]")
set res2 to (do shell script "tcl " & cmd2)
set fpat to "{([0-9A-Za-z_]+)}"
set rpat to "{Tcl_\\1} "
set cmd3 to "Tcl_regsub " & fpat & " \"" & res2 & "\" " & rpat
do shell script "tcl " & Tcl_regsub & " " & quoted form of cmd3
--> "Tcl_after Tcl_append Tcl_array Tcl_auto_execok Tcl_auto_import
Tcl_auto_load Tcl_auto_load_index Tcl_auto_qualify Tcl_binary Tcl_break
Tcl_case Tcl_catch Tcl_cd Tcl_clock Tcl_close Tcl_concat Tcl_continue
Tcl_encoding Tcl_eof Tcl_error Tcl_eval Tcl_exec Tcl_exit Tcl_expr
Tcl_fblocked Tcl_fconfigure Tcl_fcopy Tcl_file Tcl_fileevent Tcl_flush
Tcl_for Tcl_foreach Tcl_format Tcl_gets Tcl_glob Tcl_global Tcl_if
Tcl_incr Tcl_info Tcl_interp Tcl_join Tcl_lappend Tcl_lindex
Tcl_linsert Tcl_list Tcl_llength Tcl_load Tcl_lrange Tcl_lreplace
Tcl_lsearch Tcl_lset Tcl_lsort Tcl_namespace Tcl_open Tcl_package
Tcl_pid Tcl_proc Tcl_puts Tcl_pwd Tcl_read Tcl_regexp Tcl_regsub
Tcl_rename Tcl_return Tcl_scan Tcl_seek Tcl_set Tcl_socket Tcl_source
Tcl_split Tcl_string Tcl_subst Tcl_switch Tcl_tclLog Tcl_tell Tcl_time
Tcl_trace Tcl_unknown Tcl_unset Tcl_update Tcl_uplevel Tcl_upvar
Tcl_variable Tcl_vwait Tcl_while"
--#4
-- Calling Tcl regsub with options. "-all" for find all occurences and
"-nocase" for considering case
set Tcl_regsub to quoted form of "proc Tcl_regsub {searchPat inString
replacePat {theOptions \"\"} } {
if {[string length $theOptions]} {
set str [string trim $theOptions]
if {[string compare -all $str] == 0} {
regsub -all -- $searchPat $inString $replacePat res;
puts -nonewline $res;
return;
} elseif {[string compare -nocase $str] == 0} {
regsub -nocase -- $searchPat $inString $replacePat res;
puts -nonewline $res;
return;
}
}
regsub -all -nocase -- $searchPat $inString $replacePat res;
puts -nonewline $res;
}
"
set cmd1 to quoted form of ("lsort -dict [info commands]")
set res1 to (do shell script "tcl " & cmd1)
set spat to "{([0-9A-Za-z_]+)}"
set rpat to "{Tcl_\\1} "
set opts to "-all -nocase"
set cmd2 to "tcl " & Tcl_regsub & quoted form of (" Tcl_regsub " & spat
& " \"" & res1 & "\" " & rpat)
set res2 to do shell script cmd2
Philip Aker
http://www.aker.ca
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.