I do not have an M1 Mac, however, the following was tested and works on an Intel based Mac running macOS Big Sur.
The intent of the example AppleScript code, shown below, is to mimic the manual events necessary to start a slideshow of the selected items in Finder, or the contents of the window itself with nothing selected, by doing it with an AppleScript application placed in its Toolbar.
If nothing is selected in the front window when the app icon is clicked in the Toolbar, then all items are selected and the slideshow starts.
If individual files are selected in the front window when the app icon is clicked in the Toolbar, the slideshow starts with the selected individual files.
If a folder is selected in the front window when the app icon is clicked in the Toolbar, then if the folder is not already opened, its opened, the items are selected and the slideshow starts. If the folder is already opened, the items are selected and the slideshow starts. This takes into account the various views the window can be in, i.e., icon view, list view, or column view.
Note that no other manipulation of the state of the front window is attempted after the slideshow finishes because, if it was started manually, any other actions would have to be done so manually after the fact anyway. Additionally, any AppleScript method employed, if possible, to trigger something after the slideshow finishes would probably be resource intensive, having to constantly monitor its state and act accordingly. Therefore I do not consider it practical to implement, even if it's possible to do.
The example AppleScript code was saved as an AppleScript application named Start Slideshow in Script Editor, given a custom icon in Finder and added to its Toolbar, as shown in the image below.
Example AppleScript code:
tell application "Finder"
activate
set mySelection to selection
if mySelection is {} then
set selection to every item of front window
my startSlideshow()
else
set mySelectionAlias to first item of mySelection as alias
set mySelectionAliasProperties to properties of mySelectionAlias
if the class of mySelectionAliasProperties is folder then
if name of front window is equal to name of mySelectionAliasProperties then
set selection to every item of front window
my startSlideshow()
else
my openFolder()
set selection to every item of front window
my startSlideshow()
end if
else
my startSlideshow()
end if
end if
end tell
on openFolder()
tell application "System Events" to ¬
key code 31 using command down
delay 0.2
end openFolder
on startSlideshow()
tell application "System Events" to ¬
key code 49 using option down
end startSlideshow
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.