Hey Guys,
Thanks so much for your help and candor. I have a finished applet that does what I’d like. The only missing link is getting it to show up on the “open with” menu for all file types. I tried adding “*” to the CFBundleTypeExtensions in the info.plist for the finished applet and then rebuilding the launch services database, but it doesn’t show up.
One of you guys with OS X programming experience let me know how to achieve this?
(sorry, I know this particular question isn’t AS-related)
The applet is pretty simple to use. If you open a file with it for the first time then you choose an application to assign to that filetype, and subsequent opening will use result in the file being opened with the same application without any other user interaction. If you open the applet itself then you have options to add or remove file type associations, or to open files using it.
To use the applet, save the below code as an applet, and then do whatever is necessary to get it to show up on the “open with” menu for all file types (I don’t know what that is currently, but I hope to know soon!). I have mine saved as “¢ Open With” (where “¢” is a bullet [option-8 to use]) so that it appears alphabetically before letters and numbers. Pick a nice icon and you’re good to go.
property SampleList : {{path:"Applications:gedit.app", name:"gedit", BundleID:"org.gnome.gedit", TypeExtList:{"log", "script"}}}
property DefaultAppListFile : "DefaultApps.b"
property SummaryFile : "File Association Summary.txt"
property TheList : {}
on open theFiles
local FileExt, FileName, FoundAppList, OpenWithAppList, PickAppList
GetList()
repeat with aFile in theFiles
tell application "System Events"
set FileExt to name extension of aFile
set FileName to displayed name of aFile
end tell
set FoundAppList to {}
set OpenWithAppList to {}
repeat with AnApp in TheList
if TypeExtList of AnApp contains FileExt then set end of FoundAppList to AnApp
end repeat
if (count of FoundAppList) = 1 then
set OpenWithAppList to FoundAppList
else if (count of FoundAppList) > 1 then
set PickAppList to {}
repeat with AnApp in FoundAppList
set end of PickAppList to name of AnApp
end repeat
set PickAppList to choose from list PickAppList with title "Open " & FileName & " using:" default items {beginning of PickAppList} with multiple selections allowed without empty selection allowed
if PickAppList is false then return 0
repeat with PickedAppName in PickAppList
repeat with AnApp in TheList
if PickedAppName as string is (name of AnApp) as string then
set end of OpenWithAppList to AnApp
exit repeat
end if
end repeat
end repeat
else
set end of OpenWithAppList to SelectDefaultApp(missing value, {FileExt})
if end of OpenWithAppList is false then return 0
end if
repeat with AnApp in OpenWithAppList
if BundleID of AnApp is not missing value then
tell application "Finder" to open aFile using application file id (BundleID of AnApp)
else
tell application (path of AnApp)
activate
open aFile
end tell
end if
end repeat
end repeat
end open
on run
local ActionList, AddExt, RemExt, RemApp, OpenFiles, ViewSummary
local TmpList, TmpStr, TmpButton
set AddExt to "Add File Type Association"
set RemExt to "Remove File Type Association"
set RemApp to "Remove Application Association"
set OpenFiles to "Open File(s)"
set ViewSummary to "View File Type Association Summary"
repeat
GetList()
if (count of TheList) > 0 then
set ActionList to {AddExt, RemExt, RemApp, ViewSummary, OpenFiles}
else
set ActionList to {AddExt, OpenFiles}
end if
set TheAction to choose from list ActionList with prompt "What would you like to do?" with title "Open With..." default items {AddExt} without multiple selections allowed and empty selection allowed
if TheAction is false then return 0
set TheAction to beginning of TheAction
if TheAction is AddExt then
set {TmpStr, TmpButton} to {text returned, button returned} of (display dialog "Enter file extension(s) to add association for:" & return & return & "(separate multiple extensions with spaces)" default answer "" with icon note)
if TmpButton is "Cancel" then return 0
set TmpStr to parseLine(TmpStr, space)
set TmpList to GetTmpList(false)
set NewApp to "Select new application"
if (count of TmpList) > 0 then
set beginning of TmpList to NewApp
set TmpList to choose from list TmpList with prompt "Add file association(s) to which application(s)?" default items {beginning of TmpList} with multiple selections allowed and empty selection allowed
if TmpList is false then return 0
else
set TmpList to {NewApp}
end if
repeat with AnApp in TmpList
if AnApp as string is NewApp as string then
SelectDefaultApp(missing value, TmpStr)
else
SelectDefaultApp(AnApp, TmpStr)
end if
end repeat
else if TheAction is RemExt then
set TmpList to GetTmpList(true)
if (count of TmpList) > 1 then
set TmpStr to choose from list TmpList with prompt "Which file type extension would you like to remove?" default items {beginning of TmpList} without multiple selections allowed and empty selection allowed
if TmpStr is false then return 0
set TmpStr to beginning of TmpStr
else
set TmpStr to beginning of TmpList
end if
set TmpList to {}
repeat with AnApp in TheList
if TypeExtList of AnApp contains TmpStr then set end of TmpList to name of AnApp
end repeat
if (count of TmpList) > 1 then
sort(TmpList, 1, -1, {})
set TmpList to choose from list TmpList with prompt "Remove " & TmpStr & " association from which applications?" default items {beginning of TmpList} with multiple selections allowed without empty selection allowed
if TmpList is false then return 0
end if
repeat with ByeApp in TmpList
repeat with i from 1 to count of TheList
if name of (item i of TheList) as string is ByeApp as string then
if (count of TypeExtList of item i of TheList) > 1 then
set TypeExtList of item i of TheList to RemoveFromList(TypeExtList of item i of TheList, {TmpStr})
else
set TheList to RemoveFromList(TheList, {item i of TheList})
end if
exit repeat
end if
end repeat
end repeat
SaveList()
else if TheAction is RemApp then
set TmpList to GetTmpList(false)
if (count of TmpList) > 1 then
set TmpStr to choose from list TmpList with prompt "Which application would you like to remove association(s) for?" default items {beginning of TmpList} without multiple selections allowed and empty selection allowed
if TmpStr is false then return 0
set TmpStr to beginning of TmpStr
else
set TmpStr to beginning of TmpList
end if
repeat with i from 1 to count of TheList
if name of item i of TheList is TmpStr then
if (count of TypeExtList of item i of TheList) > 1 then
sort(TypeExtList of item i of TheList, 1, -1, {})
set TmpList to choose from list TypeExtList of item i of TheList with prompt "Remove which file type associations from " & name of item i of TheList & "?" default items {beginning of TmpList} with multiple selections allowed without empty selection allowed
if TmpList is false then return 0
if (count of TmpList) < (count of TypeExtList of item i of TheList) then
set TypeExtList of item i of TheList to RemoveFromList(TypeExtList of item i of TheList, TmpList)
else
set TheList to RemoveFromList(TheList, {item i of TheList})
end if
else
set TheList to RemoveFromList(TheList, {item i of TheList})
end if
exit repeat
end if
end repeat
SaveList()
else if TheAction is ViewSummary then
set TmpStr to (((path to me) as text) & "Contents:Resources:" & SummaryFile)
tell application "Finder"
activate
reveal TmpStr
end tell
return 0
else if TheAction is OpenFiles then
open (choose file with multiple selections allowed)
return 0
end if
end repeat
end run
on GetList()
local ListPath, ListFile
set ListPath to (((path to me) as text) & "Contents:Resources:" & DefaultAppListFile)
set ListFile to open for access ListPath -- with write permission
--set eof of ListFile to 0
--write SampleList to ListFile as list
set TheList to {}
try
set TheList to read ListFile from 0 as list
end try
close access ListFile
end GetList
on SaveList()
local ListPath, ListFile
set ListPath to (((path to me) as text) & "Contents:Resources:" & DefaultAppListFile)
set ListFile to open for access ListPath with write permission
set eof of ListFile to 0
write TheList to ListFile as list
close access ListFile
set ListPath to (((path to me) as text) & "Contents:Resources:" & SummaryFile)
set ListFile to open for access ListPath with write permission
set eof of ListFile to 0
if (count of TheList) > 0 then
repeat with AnApp in TheList
write (name of AnApp & " (" & name of AnApp & ", located at " & path of AnApp & ")" & return & "File types: ") as text to ListFile
repeat with i from 1 to count of TypeExtList of AnApp
write (item i of TypeExtList of AnApp) as text to ListFile
if i < (count of TypeExtList of AnApp) then write ", " as text to ListFile
end repeat
write (return & return) as text to ListFile
end repeat
else
write "There are no file associations in effect." to ListFile
end if
close access ListFile
end SaveList
on SelectDefaultApp(NewAppName, FileExts)
local SelectedApp, AppPath, AppName, AppBundleID, NewApp, ListModified
if NewAppName is missing value then
set SelectNewApp to true
set SelectedApp to choose file with prompt "Select application to add file association to" default location (path to applications folder) of type {"app"} without multiple selections allowed
if SelectedApp is false then return false
set AppPath to SelectedApp as text
if text item -1 of AppPath is ":" then repeat until text item -1 of AppPath is not ":"
set AppPath to (text items 1 through -2 of AppPath) as text
end repeat
else
set SelectNewApp to false
end if
repeat with AnApp in TheList
if (SelectNewApp and AppPath is path of AnApp) or (not SelectNewApp and NewAppName as string is name of AnApp as string) then
set ListModified to false
repeat with AnExt in FileExts
if TypeExtList of AnApp does not contain AnExt then
set end of TypeExtList of AnApp to AnExt
set ListModified to true
end if
end repeat
if ListModified then
SaveList()
return AnApp
end if
end if
end repeat
if SelectNewApp then
tell application "System Events" to set AppName to displayed name of SelectedApp
set AppBundleID to missing value
try
tell application "Finder" to set AppBundleID to id of application file AppPath
end try
set NewApp to {path:AppPath, name:AppName, BundleID:AppBundleID, TypeExtList:FileExts}
set end of TheList to NewApp
SaveList()
return NewApp
end if
end SelectDefaultApp
on RemoveFromList(OldList, RemItemList)
local NewList
set NewList to {}
if class of beginning of RemItemList is record then
repeat with i from 1 to count of OldList
set RemItem to false
repeat with j from 1 to count of RemItemList
if name of item i of OldList is name of item j of RemItemList then
set RemItem to true
exit repeat
end if
end repeat
if not RemItem then set end of NewList to item i of OldList
end repeat
else
repeat with i from 1 to count of OldList
if item i of OldList is not in RemItemList then set end of NewList to item i of OldList
end repeat
end if
return NewList
end RemoveFromList
on GetTmpList(MakeExtList)
local TmpList, Tmp
set TmpList to {}
if not MakeExtList then
repeat with AnApp in TheList
set end of TmpList to name of AnApp
end repeat
else
repeat with AnApp in TheList
repeat with AnExt in TypeExtList of AnApp
if TmpList does not contain AnExt as string then set end of TmpList to AnExt as string
end repeat
end repeat
end if
sort(TmpList, 1, -1, {})
return TmpList
end GetTmpList
on parseLine(theLine, delimiter)
-- This came from Nigel Garvey
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {delimiter}
set theTextItems to theLine's text items
set AppleScript's text item delimiters to astid
repeat with i from 1 to (count theTextItems)
if (item i of theTextItems is "") then set item i of theTextItems to missing value
end repeat
return theTextItems's every text
end parseLine
on CustomMergeSort(ListToSort, l, r, customiser) -- Sort items l thru r of theList.
script o
property comparer : me
property slave : me
property lst : ListToSort
property rangeB : missing value
on msrt(l, r)
-- Get indices for the end of the left half of this range and the beginning of the right.
set m1 to (l + r) div 2
set m2 to m1 + 1
sortHalf(l, m1) -- Sort the left half.
sortHalf(m2, r) -- Sort the right half.
-- Extract a copy of the whole range.
set rangeB to items l thru r of my lst
slave's extract(l, r)
-- Merge the already sorted halves of the extract back into the original: for each slot
-- in the original, compare the lowest unassigned item from the left half of the extract
-- with that from the right half and assign the lower of the two to the slot.
-- Indices relative to the extract.
set i to 1 -- Beginning of left half.
set endLeft to m2 - l -- End of left half.
set j to endLeft + 1 -- Beginning of right half.
set endRight to r - l + 1 -- End of right half.
-- Get the first item from the left and right halves of the extract.
set lv to beginning of my rangeB
set rv to item j of my rangeB
repeat with k from l to r
if (comparer's isGreater(lv, rv)) then
-- The right value's less than the left. Assign it to this slot in the original list.
set item k of my lst to rv
slave's merge(j, k)
-- Get the next right value. If none, assign the remaining left values to the remaining slots and exit this repeat and recursion level.
if (j < endRight) then
set j to j + 1
set rv to item j of my rangeB
else
repeat with k from (k + 1) to r
set item k of my lst to item i of my rangeB
slave's merge(i, k)
set i to i + 1
end repeat
exit repeat
end if
else
-- The left value's less than or equal to the right. Assign it to this slot in the original list.
set item k of my lst to lv
slave's merge(i, k)
-- Get the next left value. If none, simply exit this repeat and recursion level, as the remaining right values are from the remaining slots anyway.
if (i < endLeft) then
set i to i + 1
set lv to item i of my rangeB
else
exit repeat
end if
end if
end repeat
end msrt
-- Sort the "half-range" l thru r.
on sortHalf(l, r)
-- If there are more than two items in this half, recurse further to merge-sort them.
-- If there are two, simply swap them (if necessary). If one, do nothing.
if (r - l > 1) then
msrt(l, r)
else if (r > l) then
set lv to item l of my lst
set rv to item r of my lst
if (comparer's isGreater(lv, rv)) then
set item l of my lst to rv
set item r of my lst to lv
slave's swap(l, r)
end if
end if
end sortHalf
-- Default comparison and slave handlers, for a normal, mergeSort-style sort.
on isGreater(a, b)
(a > b)
end isGreater
on extract(a, b)
end extract
on merge(a, b)
end merge
on swap(a, b)
end swap
end script
-- Process the input parmeters.
set listLen to (count ListToSort)
if (listLen > 1) then
-- Negative and/or transposed range indices.
if (l < 0) then set l to listLen + l + 1
if (r < 0) then set r to listLen + r + 1
if (l > r) then set {l, r} to {r, l}
-- Supplied or default customisation scripts.
if (customiser's class is record) then set {comparer:o's comparer, slave:o's slave} to (customiser & {comparer:o, slave:o})
-- Do the sort.
o's msrt(l, r)
end if
return -- nothing
end CustomMergeSort
property sort : CustomMergeSort