No need for exquisitely complicated hex string fiddling to accomplish the task, at least not in Tahoe 26.6.2.
I have found a MUCH simpler way to do the thang! And it appears to be robust!
The old way of tell application "System Events" to set default application of anAlias to disk item ... still works, but the system and Finder will not finalize the change unless the file is changed/touched! So I came up with the idea of simply changing the file modification time by one second! That did the trick! It is actually sufficient to just set a label to the file.
However, there is a case where that alone will fail: I you have a fresh file (in my case a pdf), and open it by its default app (in my case Preview), then close the file, and you then change the default app (in my case to Acrobat), (either manually or as described above), macOS will change to the new app in Get info, but the system will also (due to security fanaticism) add the file to quarantine (so that if you try to open it, the system will claim that the file is damaged/dangerous and should be moved to Trash). Therefore, we need to remove it from quarantine. However, if it is not quarantined, the routine to remove it from quarantine will exit with a non-zero status and throw an error, so we embed it in a try block.
The result is that it seems to always work, regardless of the history of the file! I have not found a case where this would fail! I have enclosed my script here so you can see for yourself.
use AppleScript version "2.5" -- El Capitan (10.11) or later
use scripting additions --must be here if there is any other 'use' statement
-- @ Harald E Brandt
-- It will set the selected pdf files to open in Acrobat Pro
-- It will also set the label of the file to green so as to indicated that this file has been treated
-- Use by selecting files and then run it, or run it without selection and it will ask for files. If a folder is selected it will treat all pdf files in that folder.
-- it will increase the file's modification time by one second
tell application "Finder" to get the selection as alias list --aliases are more robust since also System Events understands them
if result is {} then
set aliasList to choose file with prompt "Välj pdf-filer som skall öppnas i Acrobat" of type {"com.adobe.pdf"} with multiple selections allowed
setOpenWithAcrobat(aliasList)
else
setOpenWithAcrobat(result)
end if
on setOpenWithAcrobat(aliasList)
set acrobatPath to (path to application id "com.adobe.Acrobat.Pro" as text) --note that text is necessary, otherwise it will be an alias
repeat with anAlias in aliasList
tell application "System Events" to set kindOfFile to kind of anAlias
if kindOfFile is "Folder" then
tell application "System Events" to get every alias of anAlias
setOpenWithAcrobat(result)
else if kindOfFile is "PDF document" then
tell application "System Events" to set default application of anAlias to disk item acrobatPath --note that since Mavericks, the property default application no longer accepts alias but it does accept disk item
tell application "System Events" to set modification date of anAlias to (modification date of anAlias) + 1
tell application "Finder" to set label index of anAlias to 6 --integer 0-7 are: none, orange, red, yellow, blue, purple, green, grey
--tell application "Finder" to update item anAlias --does not appear to be needed, and it cannot replace the above 2 lines that touches the file.
-- If the file earlier had been opened by its previous default app, and you then change the default app, macOS will change to the new app in Get info, but the system will also add the file to quarantine (so that if you try to open it, the system will claim that the file is damaged/dangerous and should be moved to Trash). Therefore, we need to remove it from quarantine. However, if it is not quarantined, it will exit with a non-zero status and throw an error, so we embed it in a try block:
try
set targetPath to quoted form of (POSIX path of anAlias) --beware not to have this inside a tell application "System Events" block since that will throw an error (Can’t make quoted form of POSIX path of alias "<the path>" into type specifier). It's a type problem, but can be remedied by adding 'as text'
set removeQuarantineScript to "xattr -d com.apple.quarantine " & targetPath
do shell script removeQuarantineScript
end try
end if
end repeat
end setOpenWithAcrobat