Skip to content

Instantly share code, notes, and snippets.

@grmartin
Created May 11, 2019 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grmartin/afbc9ed3d416661c01eb709af554ba9a to your computer and use it in GitHub Desktop.
Save grmartin/afbc9ed3d416661c01eb709af554ba9a to your computer and use it in GitHub Desktop.
Launch Firefox Profile Droplet
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
on open theFiles
repeat with aFile in theFiles
processAFile(contents of aFile)
end repeat
end open
on processAFile(theFile)
local pathsToTry, environ, fireFoxBin, switches, logTo
set pathsToTry to ¬
the POSIX path of (((path to library folder from user domain) as string) & "::"), ¬
the POSIX path of (((path to me as string) & "::")), ¬
the POSIX path of theFile ¬
}
set environ to concatEnvironment(pathsToTry)
set fireFoxBin to getVal(environ, "GMLP_FIREFOX_BIN", "/Applications/Firefox.app/Contents/MacOS/firefox-bin")
set switches to getVal(environ, "GMLP_FIREFOX_SW", "")
set logTo to getVal(environ, "GMLP_FIREFOX_LOG", "/dev/null")
tell application "Finder"
if kind of theFile is "folder" then
set filePath to (the POSIX path of theFile)
do shell script fireFoxBin & " " & switches & " -Profile '" & filePath & "' &> " & logTo & " & "
end if
end tell
end processAFile
-- ----------------------------------------------------------------------------------------------------- -
-- Local Helper Functions
-- ----------------------------------------------------------------------------------------------------- -
on concatEnvironment(pathsToTry)
local asevnFileName, returnVal
set asevnFileName to "as.env"
set returnVal to newNSDictionary()
repeat with nextPath in pathsToTry
set actualNextPath to (nextPath & "/" & asevnFileName)
if fileExists(actualNextPath) then
set returnVal to concatNSDictionary(returnVal, toRecords(readFileLines(actualNextPath), "="))
end if
end repeat
return returnVal
end concatEnvironment
on getVal(vals, valName, default)
set sel to getRecValue(upperCase(trimString(valName)), vals)
if sel is missing value then
return trimString(default) as text
end if
return trimString(sel) as text
end getVal
on fileExists(filePath)
tell application "System Events"
if exists file filePath then
return true
else
return false
end if
end tell
end fileExists
on readFileLines(filePath)
local returnVal
set returnVal to {}
set fileReference to open for access filePath
set fileLines to paragraphs of (read fileReference)
repeat with nextLine in fileLines
if length of nextLine is greater than 0 then
copy nextLine to the end of returnVal
end if
end repeat
close access fileReference
return returnVal
end readFileLines
on toRecords(arrayLines, delim)
local returnVal
set returnVal to newNSDictionary()
repeat with nextLine in arrayLines
local kvp
set kvp to (splitString(contents of nextLine, "="))
if (count of kvp) is 2 then
set returnVal to setRecValue(upperCase(item 1 of kvp), (item 2 of kvp), returnVal)
end if
end repeat
return returnVal
end toRecords
on trimString(str)
return (current application's NSString's stringWithString:str)'s stringByTrimmingCharactersInSet:(current application's NSCharacterSet's whitespaceAndNewlineCharacterSet)
end trimString
on upperCase(str)
return (current application's NSString's stringWithString:str)'s uppercaseString
end upperCase
on splitString(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitString
on newNSDictionary()
return (current application's NSDictionary's dictionary)
end newNSDictionary
on concatNSDictionary(a, b)
set returnVal to (current application's NSMutableDictionary's dictionary)
returnVal's addEntriesFromDictionary:a
returnVal's addEntriesFromDictionary:b
return returnVal
end concatNSDictionary
-- ----------------------------------------------------------------------------------------------------- -
-- Records to Mutable Dictionaries
-- ----------------------------------------------------------------------------------------------------- -
-- https://forum.keyboardmaestro.com/t/making-applescript-records-dynamic-using-asobjc/4082
on getRecKeyList(pRecord)
(*
PARAMETER: pRecord -- AppleScript Record
RETURNS: List of text items of Keys in Record
*)
return ¬
(current application's NSDictionary's ¬
dictionaryWithDictionary:pRecord)'s allKeys() as list
end getRecKeyList
on getRecValue(pKeyStr, pRecord)
(*
VER: 1.1.2 2016-06-13
PARAMETERS:
• pKeyStr : Record Key (string)
• pRecord : Record to be searched
RETURNS: Value for Specified Key (in class of that value)
• This can be text, number, list, record, whatever
*)
local theDict, theResult, tempArray
set theDict to current application's NSDictionary's dictionaryWithDictionary:pRecord
set theResult to theDict's objectForKey:pKeyStr
--- Convert ASObjC Class of theResult to AppleScript Class ---
-- This covers text, numbers, lists, records, whatever,
-- including missing value
-- (per Shane Stanley 2016-06-13)
set tempArray to current application's NSArray's arrayWithArray:{theResult}
return item 1 of (tempArray as list)
end getRecValue
(*
NOTE FROM SHANE STANLEY (2016-06-13) (paraphrased)
Instead of checking for class, use this, which also handles missing value:
set theResult to theDict’s objectForKey:pKeyStr
set someArray to current application’s NSArray’s arrayWithArray:{theResult}
return item 1 of (someArray as list)
What this does is makes a single-item array containing theResult.
When that’s converted to an AppleScript list, the item within it
will also be converted to an AppleScript item if it can.
So this covers all the bridgeable AS classes
– text, numbers, lists, records, whatever [including missing value]
You then extract the item from the list.
*)
-- https://forum.keyboardmaestro.com/t/making-applescript-records-dynamic-using-asobjc/4082/5
on setRecValue(strKey, varValue, rec)
set ca to current application
set nsDct to (ca's NSMutableDictionary's dictionaryWithDictionary:rec)
nsDct's setValue:varValue forKey:strKey
item 1 of ((ca's NSArray's arrayWithObject:nsDct) as list)
end setRecValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment