[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Update to cli4mp (AppleScript front end)



For anyone who cares, I've updated my "cli4mp" AppleScript to be a bit
more friendly and fix a glaring ommision. The changes are documented in
the revision comments. One of the changes should allow the script to
pass through emailers OK, now, though it still uses some special
characters. Sorry, some of the lines are longer than 80 characters, so
watch out for forced line breaks. Just copy the source (below) into a
new script window and save as an application. Read the comments for more
details.

Let me know if you have any problems, complaints, or suggestions...

jay


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

-- cli4mp
--
-- v1.1    (24 Mar 1999)
--        > AppleScript no longer waits for the MacPerl script to terminate.
--        > New I/O directive added for choosing an output file.
--        > I/O directives can now be quoted with \.
--        > I/O directives changed to be intuitive, yet email friendly.
--        > Removed clunky / funky file loops. I think users would very rarely
--           need to input an arbitrary number of files or folders and
if they would,
--           they would prefer file globbing with wild cards!  Uhh... no
I haven't
--           implemented that yet!
--
-- v1.0    (11 Mar 1999)
--        Original version.
-- 
-- What it does, in a nutshell:
--
--  (1) The user drops a perl script on the cli4mp icon.
--  (2) cli4mp puts up a dialog prompting for the command line arguments.
--  (3) cli4mp tells MacPerl to execute the dropped perl script using the
--        command line as arguments.
--  (4) MacPerl does whatever your script tells it to with the ARGV list.
--
-- However, it's not quite that simplistic. First, cli4mp passes most characters
-- to MacPerl indiscriminately, with a few exceptions.
--
--  (1) Space characters are the argument delimiters and are not passed unquoted.
--  (2) The backslash (\) is the quote character and forces the next immediate
--      character to be inserted into an argument.
--  (3) There are three special characters that behave as I/O directives.
--        (a) The Option-backslash character (Ç) tells cli4mp to request
an input file
--             from the user with a StandardFile dialog. Use a '\' to
quote Ç.
--        (b) The Option-shift-backslash character (È) tells cli4mp to
request an
--             output file from the user with a StandardFile dialog. Use
a '\' to quote È.
--       (c) The Option-f character (°) tells cli4mp to request a folder from
--            the user with a StandardFile dialog. Use a '\' to quote °.
--
-- Well, I've done some reasonably thorough testing, but I can't
guarantee that
-- there's not something still broken in there somewhere. Feel free to
send me bug
-- reports.
--
-- LIMITATIONS
--    >  Only one file can be dropped on cli4mp at a time.
--    >  Dropped files must be a valid, functional perl script... duh!
--    >  Both AppleScript and MacPerl must be installed... double duh!
-- 
-- As usual, no warrantees expressed or implied.
-- Jay Bedsole   rwdd30@email.sps.mot.com


on open theScript
	
	global theArgs, currentWord
	
	if the length of theScript is greater than 1 then
		display dialog "Sorry, I only know how to handle one script at a
time!" buttons Â
			"OK" default button 1
	else
		-- ask the user for the command line arguments to be passed to the
MacPerl script	
		display dialog "Enter ARGV arguments:" default answer ""
		set theCmdLine to the text returned of result
		
		-- since AppleScript has a different idea of what a word is from what
we need,
		-- I'm parsing the 'command line' character by character.
		
		set theArgs to {} -- initialize the argument list
		set currentWord to "" -- initialize the current word
		set startChar to 1 -- initialize the starting position for theCmdLine
		
		-- The next section of code was the best way I could think of to
prevent the code
		-- in the repeat loop from getting errors when it tried to look at
character 0.
		-- Note: This language could really use a 'next' statement!!!
		if character 1 of theCmdLine is " " then set startChar to 2
		if character 1 of theCmdLine is "\\" then set startChar to 2
		if character 1 of theCmdLine is "Ç" then
			set theFile to new file with prompt "Specify output file to add to ARGV."
			set theArgs to (theArgs & {theFile})
			set startChar to 2
		end if
		if character 1 of theCmdLine is "È" then
			set theFile to new file with prompt "Specify output file to add to ARGV."
			set theArgs to (theArgs & {theFile})
			set startChar to 2
		end if
		if character 1 of theCmdLine is "°" then
			set theFolder to choose folder with prompt "Select folder to add to ARGV."
			set theArgs to (theArgs & {theFolder})
			set startChar to 2
		end if
		
		
		-- Here's the main loop
		repeat with i from startChar to the number of characters in theCmdLine
			
			-- Look for input file directive
			if character i of theCmdLine is "Ç" and character (i - 1) of
theCmdLine Â
				is not "\\" then -- Check for quoted option-backslash
				set theFile to choose file with prompt "Select file to add to ARGV."
				set theArgs to (theArgs & {theFile})
				
				-- Look for output file directive
			else if character i of theCmdLine is "È" and character (i - 1) of
theCmdLine Â
				is not "\\" then -- Check for quoted option-shift-backslash
				set theFile to new file with prompt "Specify output file to add to ARGV."
				set theArgs to (theArgs & {theFile})
				
				-- Look for select folder directive
			else if character i of theCmdLine is "°" and character (i - 1) of
theCmdLine Â
				is not "\\" then -- Check for quoted option-f
				set theFolder to choose folder with prompt "Select folder to add to ARGV."
				set theArgs to (theArgs & {theFolder})
				
				-- Word break on unquoted spaces
			else if character i of theCmdLine is " " and character (i - 1) of
theCmdLine Â
				is not "\\" then -- Check for quoted space
				if currentWord is not "" then -- Check for empty word
					set theArgs to (theArgs & {currentWord}) -- Completed word
					set currentWord to "" -- reinitialize the current word
				end if
				
				-- Otherwise add the character to the current word
			else
				if character i of theCmdLine is "\\" then -- Check for quoted characters
					-- Next check for quoted quote
					if character (i - 1) of theCmdLine is "\\" then Â
						set currentWord to Â
							(currentWord & character i of theCmdLine)
				else
					set currentWord to Â
						(currentWord & character i of theCmdLine)
				end if
			end if
		end repeat
		
		-- Add last word if not empty
		if currentWord is not "" then set theArgs to (theArgs & {currentWord})
		
		
		-- now run the MacPerl script with the arguments
		ignoring application responses -- AppleScript won't wait for MacPerl
			tell application "MacPerl"
				Do Script {theScript as Text} & theArgs
			end tell
		end ignoring
	end if
	
end open

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org