I’ve never created a shortcut that utilizes AppleScript’s GUI scripting and wondered if this was possible. The answer is mostly yes, but it requires some work.
The most difficult part of this is getting permissions set. Accessibility and full-disk-access permissions have to be set for the Shortcuts app, siriactionsd, and System Events. This tends to be the default and, in any event, only has to be set once.
If the shortcut is run by way of a Finder toolbar icon or keyboard shortcut, accessibility and possibly full-disk-access permissions will have to be set for the shortcut app created to run the specific shortcut. Typically this app will be in the User’s Applications folder.
The test shortcut included below uses GUI scripting to close all subfolders in a folder selected in a Finder window. The same task can often be accomplished by way of the option-left-arrow keyboard shortcut, and this is probably a better approach for most people. The option-right-arrow code was included to workaround a circumstance where the option-left-arrow code alone does not close all subfolders.
Close Folders.shortcut (21.7 KB)