Tip: PHP CLI and AppleScript

I’ve been meaning to get to this for some time, if you install the latest PHP package from the outstanding Marc Liyanage http://www.entropy.ch/phpbb2/viewtopic.php?t=137, then you can incorporate PHP directly into AppleScript which opens up an amazing amount of versatility:

set the_string to "This is a very long line of text that keeps going and going..."
set max_chars_er_line to 15
set the_code to "echo wordwrap(\"" & the_string & "\", " & (max_chars_er_line as string) & ");"
set the_string to my php_execute(the_code)

my php_execute(the_code) --exceute PHP code
my php_execute_su(the_code) --execute PHP code as admin
my php_validate_file(the_file) --validate a PHP text file
my php_get_help() --get help with the PHP CLI
my php_get_info() --get info on the installed PHP package
my php_get_modules() --get the names of all installed PHP modules
my php_get_module_functions(the_module) --get the functions of a specific module
my php_get_all_functions() --return all the functions of all the installed PHP modules, not very speedy...
my php_search_manual(the_term) --search the online maunal for a PHP term

--***********************************-- main php calls --***********************************--

on php_execute(the_code)
		return do shell script "/usr/local/php/bin/php -r " & (quoted form of the_code)
	on error the_err
		return {the_err, the_code}
	end try
end php_execute

on php_execute_su(the_code)
		return do shell script "/usr/local/php/bin/php -r " & (quoted form of the_code) with administrator privileges
	on error the_err
		set root_pw to ""
		return {the_err, the_code}
	end try
end php_execute_su

on php_validate_file(the_file)
		return do shell script "/usr/local/php/bin/php -l " & (quoted form of POSIX path of the_file)
	on error the_err
		return the_err
	end try
end php_validate_file

on php_get_help()
		return do shell script "/usr/local/php/bin/php -h"
	on error the_err
		return the_err
	end try
end php_get_help

on php_get_info()
	return my php_execute("echo phpinfo();")
end php_get_info

on php_get_modules()
	return my string_to_list((my php_execute("echo implode(get_loaded_extensions(), ", ");")), ", ")
end php_get_modules

on php_get_module_functions(the_module)
	return my string_to_list((my php_execute("echo implode(get_extension_funcs(\"" & the_module & "\"), ", ");")), ", ")
end php_get_module_functions

on php_get_all_functions()
	set the_modules to my php_get_modules()
	set all_functions to {}
	repeat with i from 1 to count of the_modules
		set all_functions to all_functions & my php_get_module_functions(item i of the_modules)
	end repeat
	return all_functions
end php_get_all_functions

on php_search_manual(the_term)
	open location ("http://www.php.net/manual-lookup.php?pattern=" & the_term & "?=en")
end php_search_manual

on string_to_list(the_string, the_delim)
	set AppleScript's text item delimiters to {the_delim}
	set the_list to every text item of the_string as list
	set AppleScript's text item delimiters to {""}
	return the_list
end string_to_list


that’s pretty sweet :smiley: