content: -- iTerm2-Finder-Droplet by Psobko
  -- https://github.com/psobko/iterm2-finder-droplet
  
  -- Run handler : opens current folder
  on run
  	try
  		tell application "Finder"
  			set folder_to_process to quoted form of (POSIX path of ((folder of the front window) as Unicode text))
  		end tell
  		process_folder(folder_to_process)
  	on error errMsg number errNum
  		display alert "Error:" & errMsg & number & errNum
  	end try
  	
  	quit me
  end run
  
  -- Open handler: process dropped items
  on open dropped_item
  	try
  		if (count of dropped_item) is not equal to 1 then
  			display alert "Only single folders can be dropped."
  			return
  		end if
  		
  		set item_info to info for dropped_item
  		
  		if item_info is package folder or item_info is folder then
  			log "Folder dropped"
  			--or dropped_item is Volume then
  			--kind of dropped_item is "Folder" or kind of dropped_item is "Volume" then
  			tell application "Finder"
  				set folder_to_process to quoted form of (POSIX path of (dropped_item as Unicode text))
  			end tell
  			process_folder(folder_to_process)
  			return
  		end if
  		
  		display alert "Only folders and packages can be dropped."
  		
  	on error errMsg number errNum
  		display alert "Error:" & errMsg & number & errNum
  	end try
  	
  	quit me
  end open
  
  -- Sub Routines --
  
  -- Pass the folder to iTerm
  on process_folder(selected_folder)
  	if not application_exists("iTerm") then
  		error "Can't find iTerm application :("
  	end if
  	
  	tell application "iTerm"
  		activate
  	end tell
  	
  	set script_modifier to ""
  	
  	if modifierPressed("option") then -- open in new window
  		set script_modifier to "
  		create window with default profile" & "
  		end tell" & "
  		tell application \"iTerm2\" "
  		
  	else if modifierPressed("command") then --open in new tab
  		set script_modifier to "
  		tell current window" & "
  		create tab with default profile " & "
  		end tell"
  		
  	else if modifierPressed("control") then --open in current tab + clear
  		-- set script_modifier to "
  		-- tell current session of current window" & "
  		-- write text \"cd /tmp/;clear;\" "
  		
  	end if --open in current tab
  	-- set script_modifier to "
  	-- tell current session of current window" & "
  	-- write text \"cd /tmp/;\" "
  	
  	set full_script to "tell application \"iTerm2\" " & script_modifier & " 
  						tell current session of current window" & "
  							write text \"cd " & selected_folder & ";\" " & "
  						end tell" & "
  					end tell"
  	--------DEBUG--------------------------
  	-- display dialog "script: " + full_script
  	------------------------------------------
  	run script full_script
  end process_folder
  
  -- Check application is installed
  -- based on: https://discussions.apple.com/thread/2118214?tstart=0
  on application_exists(application_name)
  	tell application "Finder" to set does_exist to exists application file ((path to applications folder as string) & application_name)
  	return does_exist
  end application_exists
  
  -- Check for pressed modifier keys
  -- based on: http://macscripter.net/viewtopic.php?id=33652
  on modifierPressed(checkKey)
  	if checkKey = "" or checkKey = "option" or checkKey = "alt" then
  		if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSAlternateKeyMask '") > 1 then
  			return true
  		end if
  	end if
  	
  	if checkKey = "" or checkKey = "command" then
  		if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSCommandKeyMask '") > 1 then
  			return true
  		end if
  	end if
  	
  	if checkKey = "" or checkKey = "shift" then
  		if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSShiftKeyMask '") > 1 then
  			return true
  		end if
  	end if
  	
  	if checkKey = "" or checkKey = "control" then
  		if (do shell script "/usr/bin/python -c 'import Cocoa; print Cocoa.NSEvent.modifierFlags() & Cocoa.NSControlKeyMask '") > 1 then
  			return true
  		end if
  	end if
  	return false
  end modifierPressed