AppleScript - Bare Basics

This is flawed, incomplete, probably violates copyright laws and so on and so forth… but it’s a start… and it’s editable and could be saved as text to help in Spotlight searches. It’s designed to be run as an app… but can be opened and used within Script Editor.

Comments anyone?

Peter B.



on run
	my Choose_Code()
end run

-----

on Choose_Code()
	
	set paste_code to ""
	
	tell me to activate
	set code_choice to choose from list {"File Info", "Finder Tell Block", "System Events Tell Block", "System Events - Tell Process", "Process Identification", "Display Dialog - Minimal", "Display Dialog - Set Variable", "Display Dialog - With Text Entry", "Make New Folder - New File", "Read File", "Write To File", "Try - With Error Message", "Text Item Delimiters", "Simple Handler", "On Idle", "Load/Store/Run Script", "Make Script Application Wait", "Droplet (On Open - Repeat)", "Key Down - Key Up", "Command Down Example", "Keyboard Equivalents", "-----", "Special Application Bundle Preps", "-----", "More Examples", "-----", "Edit", "-----", "Quit", "-----"} with prompt "Choose a code routine..." & return with title "Code Examples"
	
	set code_choice to code_choice as text
	
	--
	
	if code_choice is "false" then
		error number -128
	end if
	
	--
	
	if code_choice is "More Examples" then
		
		tell me to activate
		set code_choice to choose from list {"-----", "Previous Examples", "-----", "Edit", "-----", "Quit", "-----"} with prompt "Choose a code routine..." & return with title "Code Examples"
		
		set code_choice to code_choice as text
		
	end if
	
	--
	
	if code_choice is "Previous Examples" then
		
		my Choose_Code()
		
	end if
	
	----------
	
	if code_choice is "File Info" then
		script file_information
			
			-- Note: All below use Standard Additions' 'info for'... some changes may be necessary within app tell blocks
			
			set chosen_file to choose file -- returns alias... add 'without invisibles' if desired
			
			set file_path to chosen_file as text -- coerce alias to string... if desired
			
			set file_info to info for chosen_file
			
			set file_name to name of file_info -- name of item with extension
			
			set displayed_name to displayed name of file_info -- (often app process name)
			
			set file_type to file type of file_info
			
			set creator_type to file creator of file_info -- (= "old" 'application file id' for Finder use)
			
			try
				set bundle_id to bundle identifier of file_info -- app bundles only (= 'application file id' for Finder use)
			on error
				set bundle_id to "(Not an app bundle)"
			end try
			
			
			set relevant_info to file_path & ", " & file_name & ", " & displayed_name & ", " & file_type & ", " & creator_type & ", " & bundle_id
			
		end script
		
		set paste_code to file_information as text
		set the clipboard to file_information as text
		
	end if
	
	--
	
	if code_choice is "Finder Tell Block" then
		
		script finder_tell_block
			
			tell application "Finder"
				
				activate -- optional if desired operation should be performed invisibly
				
				-- Finder script routine
				
			end tell
			
		end script
		
		set paste_code to finder_tell_block as text
		set the clipboard to finder_tell_block as text
	end if
	
	--
	
	if code_choice is "System Events Tell Block" then
		
		script system_events_tell_block
			
			tell application "System Events"
				
				-- System Events script routine
				
			end tell
			
		end script
		
		set paste_code to system_events_tell_block as text
		set the clipboard to system_events_tell_block as text
	end if
	
	--
	
	if code_choice is "System Events - Tell Process" then
		script system_events_tell_process
			
			tell application "TextEdit" to activate
			
			tell application "System Events"
				tell process "TextEdit"
					keystroke "n" using command down -- open new document
					--click menu item 1 of menu 1 of menu bar item 3 of menu bar 1 -- open new document alternate	
					--click menu item "New" of menu 1 of menu bar item "File" of menu bar 1 -- open new document alternate
					keystroke "Some Text"
				end tell
			end tell
			
		end script
		
		set paste_code to system_events_tell_process as text
		set the clipboard to system_events_tell_process as text
		
	end if
	
	--
	
	if code_choice is "Process Identification" then
		script process_id
			
			tell application "System Events"
				
				set all_procs to name of every process -- returns list of all user processes, including background
				
				set visible_procs to name of every process whose visible is true -- returns list of GUI processes not currently hidden
				
				set process_name to name of first process whose frontmost is true
				
			end tell
			
			
		end script
		
		set paste_code to process_id as text
		set the clipboard to process_id as text
		
	end if
	
	--
	
	if code_choice is "Display Dialog - Minimal" then
		script display_dialog_minimal
			
			tell me to activate
			display dialog "Text" & return
			
		end script
		
		set paste_code to display_dialog_minimal as text
		set the clipboard to display_dialog_minimal as text
		
	end if
	
	--
	
	if code_choice is "Display Dialog - Set Variable" then
		script display_dialog_simple
			
			tell me to activate
			display dialog "Text" & return buttons {"Cancel", "OK"} default button 2 with title "Title"
			
			set button_returned to button returned of result as text
			
		end script
		
		set paste_code to display_dialog_simple as text
		set the clipboard to display_dialog_simple as text
		
	end if
	
	--
	
	if code_choice is "Display Dialog - With Text Entry" then
		script display_dialog_text_entry
			
			tell me to activate -- optional... to bring script app to front... else activate target app
			
			display dialog "Some Text" & return & return & "Some More Text" & return default answer "Text Entry" buttons {"Cancel", "OK"} default button 2 with title "Title"
			
			--set text_returned to text returned of result
			--set button_returned to button returned of result
			
			-- Or...
			
			copy the result as list to {text_returned, button_returned}
			
		end script
		
		set paste_code to display_dialog_text_entry as text
		set the clipboard to display_dialog_text_entry as text
		
	end if
	
	--
	
	if code_choice is "Make New Folder - New File" then
		script make_new_folder_file
			
			tell application "Finder"
				
				set new_folder to make new folder at desktop with properties {name:"Test Folder"}
				set folder_name to name of new_folder
				-- open folder folder_name
				
				set new_file to make new file at desktop with properties {name:"Test"}
				set file_name to name of new_file
				-- open item file_name
				
			end tell
			
		end script
		
		set paste_code to make_new_folder_file as text
		set the clipboard to make_new_folder_file as text
		
	end if
	
	--
	
	if code_choice is "Read File" then
		script read_file
			
			set text_file to (path to desktop as text) & "Untitled.txt" as alias
			
			set text_contents to read text_file
			
		end script
		
		set paste_code to read_file as text
		set the clipboard to read_file as text
		
	end if
	
	--
	
	if code_choice is "Write To File" then
		
		script write_to_file
			
			set this_text to "This Text" -- or variable
			
			set output_file to (path to desktop as text) & "Test"
			
			try
				open for access file output_file with write permission
				write this_text & return to file output_file starting at eof
				close access file output_file
			on error
				close access file output_file
			end try
			
		end script
		
		set paste_code to write_to_file as text
		set the clipboard to write_to_file as text
	end if
	
	--
	
	if code_choice is "Try - With Error Message" then
		
		script try_error_message
			
			try
				
				--when something fails... for instance:
				
				tell application "Finder"
					open file "Foo" of the desktop
				end tell
				
				--then show the specific error and message...
				
			on error errorMsg number errorNum
				display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
			end try
			
		end script
		
		set paste_code to try_error_message as text
		set the clipboard to try_error_message as text
		
	end if
	
	--
	
	if code_choice is "Text Item Delimiters" then
		script text_item_delimiters
			
			set text_example to "bit of text"
			
			set ASTID_Priors to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {" "}
			set example_result to text items 1 through 2 of text_example as text
			set AppleScript's text item delimiters to ASTID_Priors
			
			example_result -- "bit of"
			
		end script
		
		set paste_code to text_item_delimiters as text
		set the clipboard to text_item_delimiters as text
		
	end if
	
	--
	
	if code_choice is "Simple Handler" then
		script simple_handler
			
			on simple_handler()
				-- script routine
			end simple_handler
			
			--
			
			my simple_handler() -- to call the handler elsewhere in the script
			
		end script
		
		set paste_code to simple_handler as text
		set the clipboard to simple_handler as text
		
	end if
	
	--
	
	if code_choice is "On Idle" then
		script on_idle
			
			--minimal example... must be saved as stay open application
			
			on run
				-- script routine
			end run
			
			on idle
				-- another script routine
				return 2
			end idle
			
		end script
		
		set paste_code to on_idle as text
		set the clipboard to on_idle as text
		
	end if
	
	--
	
	if code_choice is "Load/Store/Run Script" then
		script load_store_run
			
			-- Master Script
			
			on run
				my load_run("Slave.scpt")
			end run
			
			on load_run(this_scriptfile)
				set the script_path to (path to desktop as text) & this_scriptfile as alias
				set this_script to load script script_path
				set this_text to "Foo"
				set this_dialog of this_script to this_text
				store script this_script in script_path replacing yes
				run script this_script
			end load_run
			
			-----
			
			-- Slave Script  --  (uncomment and save following as 'Slave.scpt' to desktop)
			
			--property this_dialog : ""
			
			--display dialog this_dialog
			
		end script
		
		set paste_code to load_store_run as text
		set the clipboard to load_store_run as text
		
	end if
	
	--
	
	if code_choice is "Make Script Application Wait" then
		script wait_script_app
			
			-- works as 'simple' script application named 'Wait'... special preps required for app bundle (i.e. change executable and applet.rsrc names, and modify Info.plist)
			
			-- some of your script
			
			tell me to activate
			display dialog "Do it now...?" buttons {"Cancel", "Wait", "OK"} default button 3
			set button_pressed to button returned of result as text
			
			if button_pressed is "Wait" then
				tell application "System Events"
					set visible of application process "Wait" to false
					repeat until visible of application process "Wait" is true
					end repeat
				end tell
			end if
			
			-- resume script on dock click
			
			tell me to activate
			display dialog "Are you sure you're ready...?" buttons {"Cancel", "OK"} default button 2
			
			-- rest of script
			
			
		end script
		
		set paste_code to wait_script_app as text
		set the clipboard to wait_script_app as text
		
	end if
	
	--
	
	if code_choice is "Key Down - Key Up" then
		script key_down_up_examples
			
			tell application "TextEdit"
				activate
				make new document
			end tell
			
			tell application "System Events"
				if UI elements enabled then
					tell process "TextEdit"
						set frontmost to true
					end tell
					
					key down option
					keystroke "e"
					delay 1
					key up option
					keystroke "e"
					keystroke return
					
					keystroke "e" using option down
					delay 1
					keystroke "e"
					keystroke return
					
					key down shift
					keystroke "p"
					key up shift
					keystroke return
					
					key down option
					keystroke "p"
					key up option
					keystroke return
					
					key down {shift, option}
					keystroke "p"
					key up {shift, option}
					keystroke return
					
					keystroke "p" using {shift down, option down}
					keystroke return
					
				else
					--tell application "System Preferences"
					--activate
					--set current pane to pane "com.apple.preference.universalaccess"
					--display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
					--end tell
				end if
			end tell
			
			
		end script
		
		set paste_code to key_down_up_examples as text
		set the clipboard to key_down_up_examples as text
		
	end if
	
	--
	
	if code_choice is "Command Down Example" then
		script command_down_example
			
			-- Handle With Care...!!!
			
			tell application "System Events"
				key down command
				set command_down to true
			end tell
			
			if command_down is true then
				tell me to activate
				display dialog "Command Down" buttons {"OK"} default button 1 giving up after 2
			end if
			
			delay 4
			
			tell application "System Events"
				key up command
			end tell
			
			tell me to activate
			display dialog "Command Up" buttons {"OK"} default button 1
			
		end script
		
		set paste_code to command_down_example as text
		set the clipboard to command_down_example as text
		
	end if
	
	--
	
	if code_choice is "Keyboard Equivalents" then
		script keyboard_equivents
			
			-- Some useful keyboard equivalents set to variables
			
			set enter_key to ASCII character 3 -- or is it the return key?
			
			set left_arrow to ASCII character 28
			set right_arrow to ASCII character 29
			set up_arrow to ASCII character 30
			set down_arrow to ASCII character 31
			
		end script
		
		set paste_code to keyboard_equivents as text
		set the clipboard to keyboard_equivents as text
		
	end if
	
	--
	
	if code_choice is "Droplet (On Open - Repeat)" then
		script droplet_on_open_repeat
			
			-- save as application...
			
			on open these_items
				
				tell application "Finder"
					activate
					repeat with this_item in these_items
						set this_item to this_item as text
						
						-- do something with item this_item
						
					end repeat
					
				end tell
				
			end open
			
		end script
		
		set paste_code to droplet_on_open_repeat as text
		set the clipboard to droplet_on_open_repeat as text
		
	end if
	
	--
	
	if code_choice is "Other" then
		script --Another_Script
			
			--another routine
			
		end script
		
		set paste_code to Another_Script as text
		set the clipboard to Another_Script as text
		
	end if
	
	--
	
	if code_choice is "Other" then
		script --Another_Script
			
			--another routine
			
		end script
		
		set paste_code to Another_Script as text
		set the clipboard to Another_Script as text
		
	end if
	
	--
	
	if code_choice is "Other" then
		script --Another_Script
			
			--another routine
			
		end script
		
		set paste_code to Another_Script as text
		set the clipboard to Another_Script as text
		
	end if
	
	--
	
	if code_choice is "Other" then
		script --Another_Script
			
			--another routine
			
		end script
		
		set paste_code to Another_Script as text
		set the clipboard to Another_Script as text
		
	end if
	
	-----
	
	if code_choice is "Special Application Bundle Preps" then
		
		set paste_code to ""
		
		set plain_text to "		
Modify... (as necessary/desirable for given application bundle):
			
Info.plist items:

<key>CFBundleExecutable</key>
<string>applet</string>

<key>CFBundleIconFile</key>
<string>applet</string> -- must match executable name... or leave intact
			
<key>CFBundleSignature</key>
<string>aplt</string>

--<key>CFBundleIconFile</key> -- position of following element for added bundle identifier
--<string>preview.icns</string>
<key>CFBundleIdentifier</key>
<string>com.(author).(appname)</string>
				
--<key>LSRequiresCarbon</key> -- position of following element for background app
--<true/>
<key>LSUIElement</key>
<true/>

--

PkgInfo -- APPLxxxx - (xxxx must match CFBundleSignature)

--
			
Rename 'applet' -- executable file (in Mac OS folder of package)
Rename 'applet.rsrc' -- must match executable name
Rename 'applet.icns' -- must match executable name... or leave intact

--
			
Create .zip archive, trash original, test archived version

-----
----------
			
"
		
		set output_file to (path to desktop as text) & "Application Bundle Preps.txt"
		
		try
			open for access file output_file with write permission
			write plain_text to file output_file starting at 0
			close access file output_file
		on error
			close access file output_file
		end try
		
		tell application "TextEdit"
			activate
			open file output_file
		end tell
		
	end if
	
	-----
	
	if code_choice is "-----" then
		error number -128
	end if
	
	-----
	
	if code_choice is "Edit" then
		
		set path_to_me to path to me as text
		
		tell application "Script Editor"
			activate
			open file path_to_me
		end tell
		
		error number -128
		
	end if
	
	-----
	
	if code_choice is "Quit" then
		error number -128
	end if
	
	-----
	
	if paste_code is not "" then
		my paste_code()
	end if
	
end Choose_Code

----------

on paste_code()
	
	tell application "Script Editor"
		activate
		make new document
		--set bounds of front window to {190, 22, 817, 768} -- centered on 12" PowerBook display
	end tell
	
	tell application "Script Editor" to activate
	tell application "System Events"
		tell process "Script Editor"
			keystroke "v" using command down
		end tell
	end tell
	
end paste_code

-----
----------