In some cases, the app I want to use is not the default app, so I can’t do that with ⌘⇧O. In finder, I can achieve “open selected files with specific application” with the following automator service:
If all you want is to open records in a specific app then there’s no need to use AppleScript or Automator. You can assign shortcuts via system preferences or CustomShortcuts.app.
If you want to use AppleScript take a look at this example:
-- Open in Marked 2
tell application id "DNtp"
try
set theRecords to selected records
if theRecords = {} then error "Please select a Markdown record"
repeat with thisRecord in theRecords
set thisType to (type of thisRecord) as string
if thisType is in {"markdown", "«constant ****mkdn»"} then
set thisPath to path of thisRecord
tell application "Marked 2"
try
activate
open thisPath
on error error_message number error_number
if the error_number is not -128 then display alert "Marked 2" message error_message as warning
return
end try
end tell
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
(You can ignore the part that’s testing for the correct record type)
PS Not sure where you are developing your script, if it’s in Automator then that’s a bad place to write it, I think. If so use at least Script Editor.app instead (or download Script Debugger).
My app isn’t even in the “open with” menu, so… It’s weird actually, since it does support that file type.
Yeah, debugging AppleScript is real nightmare (and the same for iOS Shortcuts), that’s why I never take the time to dig into it. Please don’t take this as an excuse . I always have a hard time reading app-specific docs in Script Editor. Anyway, I will definitely give Script Debugger a try. Thx for the suggestion.
As for the script, don’t really have the time to try it now. Wait for my good news later! And @BLUEFROG, thx for the fully (not “semi-”) useful addition .
The script you provided works exactly as expected, huge thanks . I’ve also assembled a script that works both in Finder and DEVONthink as follows:
set openWithApp to application "BetterZip"
set openWithAppName to "BetterZip"
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
end tell
if "Finder" is in activeApp then
tell application "Finder"
open (get selection) using path to openWithApp
end tell
else if "DEVONthink 3" is in activeApp then
tell application "DEVONthink 3"
repeat with theRecord in (selection as list)
set thePath to path of theRecord
tell openWithApp
try
activate
open thePath
on error error_message number error_number
if the error_number is not -128 then display alert openWithAppName message error_message as warning
return
end try
end tell
end repeat
end tell
end if
The file type is .CBZ, Comicbook ZIP Archive, which is basically a .zip file renamed, and the application is BetterZip. I am sure this is not a DEVONthink-related issue, as it’s the same in Finder’s contextual menu.
After I set the default app used to open .cbz files as “YACReader”, BetterZip also disappeared from the Open With menu.
I also don’t use BetterZip as the default app for opening .zip files because BetterZip has some issue with filename encoding. (But it is useful when editing archive contents, so I still keep it for that use.) However, BetterZip is still in the Open With list of zip files.
BTW: I find DEVONthink 3’s smart rules, metadata as well as the long-standing smart group really handy in organizing my comic collection.
tell application "DEVONthink 3"
repeat with theRecord in (selection as list)
-- Do stuff with theRecord
end repeat
end tell
Using this, there is no need to set a variable to selected records and check if the list is empty.
If the list is empty, the repeat with pass that along and nothing will (logically) happen.
I didn’t even know that selected records is available. Is there any other resources recommended other than Dictionary in Script Editor when one is developing a AppleScript of DEVONthink? With Shortcuts coming to macOS, how necessary is it now to learn AppleScript well? For now, I only imitate others’ scripts, but lack much fundamental knowledge of AppleScript. I think I would have to learn from the most basic things if I want to start writing my own AppleScripts.
I learn from looking at scripts posted in these forums; also the scripts delivered with Devonthink
Even if I don’t need the script, I work through each command to learn the use
any other resources recommended
Google search. I use this often when I need a solution to a specific problem
When nothing happens that’s a problem for me. While writing scripts it happened often that I didn’t immediately grasp why nothing … nothing … ??? … That happens especially when a script does not yet work the way I want. Unnecessary confusion.
One way to prevent this is to display a dialog. However I’ve found that while writing it’s better to use one (or some) fixed test records via get record with uuid. Caveat: one should definitively not forget to test with other records afterwards (happened more than once that I initially had choosen suboptimal test records … )
A dialog is also a good idea to give other users who might use a script some feedback that explains why the script didn’t do what they expected.
PS: also don’t feel like reading such a long thread as English is not my mother tongue. Well, let me just wait and see what Shortcuts is going to bring us~