Open Emasoft opened 2 years ago
Yes, actions from a-Shell that involve the system UI (pickFolder, view...) are confusing the command sequencer. I don't think this will be easy to fix; basically the command has returned, because it is being executed in a different process.
It would be easier to write something using the Folder action from Shortcuts (and, probably, adding an a-Shell action to change directory to that specific folder).
Or set a bookmark with a name, maybe, because each "execute command" action restarts at the ~shortcuts directory (as you have noticed). There is no way to make the difference between two "execute command" actions in the same shortcut and two "execute command" actions in different shortcuts.
About the PS: Apple does provide an option to set the starting directory for the folder picker. The last time I tried, the option did not work (i.e. you can ask the system to start at a specific directory, it just won't do it).
I see. Then what about setting the folder at the beginning and end of each execute/get/set file command?
I see two ways:
FIRST WAY:
1 - All a-Shell shortcuts actions (including get, set file) get a "Run in folder:..." option, with the default value the current one (~shortcuts). You can pass a File/Folder variable to it.
2 - Internally when the a-Shell execute the command it bookmarks the folder as a temporary bookmark
3 - Then it 'jumps' to the folder (if present, otherwise it goes to ~shortcuts as usual). Returns error if folder does not exists (in such case it does NOT execute the command in any way, too dangerous).
4 - Next it executes the command/get/set in that folder
5 - Finally when the command has ended it jumps back to ~shortcuts (if it needs to) AND deletes the temporary bookmark.
SECOND WAY:
1 - Add to a-Shell the actions "Bookmark a Folder" and "Delete Folder Bookmark". Each bookmark would be a text key/value pair (compatible with Dictionary/json entry in shortcuts, so can be stored/retrieved by shortcuts). The key would accept a string and the value would accept any File/Folder variable.
2 - In the Shortcut you can pass a bookmark to all a-Shell commands (Similar to 'Run In Folder...', but taking a bookmark name as input).
3 - The command will internally jump to the bookmark (if present, otherwise it goes to ~shortcuts as usual). Returns error if folder does not exists (in such case it does NOT execute the command in any way, too dangerous).
4 - next it executes the commands/get/set/file.
5 - Finally it jumps back (if needed) to ~shortcuts and terminate the command.
6 - Then the user needs to explicitly delete the bookmark at the end of the shortcuts script with the "Delete Folder Bookmark" action (or just leave it with a unique name, hoping it would not collide with other shortcuts instances).
In this way each single process is handled separately, but can still share the same folder. Regarding permissions, it should work the same way the 'Save File..' action works in the shortcuts (I guess it asks the user for permission with a system dialog every time it is being asked to open/read/write in a new folder, and remembers it). What do you think? Is it feasible?
If I can create an action to "Bookmark a folder as boorkmarkName", then you can just start the list of commands in "Execute command" with "cd ~bookmarkName". Would that no be sufficient?
If I can create an action to "Bookmark a folder as boorkmarkName", then you can just start the list of commands in "Execute command" with "cd ~bookmarkName". Would that no be sufficient?
For commands executed maybe yes (but I still don't know if the folder change with cd/jump is effective, sometimes after jump I give the pwd command and it returns the old folder ). But for Get/Set you need to add the option to set the folder.
Okay, so I've got bad news and I've got good news.
foldersize.txt
in the directory selected by pickFolder, and that the "Get file" action is getting the file from the main shortcuts directory (where the file was not, hence the "file not found" error).I've replaced your commands in the main group by:
rm -f foldersize.txt
pickFolder
pwd > foldersize.txt
du -schx --apparent-size . | sort -hr >> foldersize.txt
mv foldersize.txt ~shortcuts
open shortcuts://
and it now works. It also works with:
rm -f foldersize.txt
pickFolder
pwd > ~shortcuts/foldersize.txt
du -schx --apparent-size . | sort -hr >> ~shortcuts/foldersize.txt
open shortcuts://
Done! The trick works like a charm! π https://routinehub.co/shortcut/12460/ Thank you! π
P.S. BTW I can't believe we cannot pass a folder as a param to an action, not even as a uri string. What is Apple thinking? There must be a way, this shortcut is best used from the share menu inside the Files app. Having to navigate to the folder each time is a bother...
I have written this simple shortcut to calculate a folder size:
https://www.icloud.com/shortcuts/bc135fad3a3948bc854ec88f76c4df60
Unfortunately it does't work. It seems a bug with the "execute shell command" action when pickFolder is used. After pickFolder is used a-Shell returns immediately and does not execute the subsequent commands. Then the "Get File" action does't find the file. But even if I try to execute the command twice, after getting back to shortcuts app, the folder is NOT the one chosen by the user. Even if the shell prompt shows the selected folder, the commands executed are executed in the ORIGINAL folder (Shortcuts). Please fix this. Otherwise there is no way to calculate a folder size with a-Shell. And that is a pity, because the standard get folder content action of siri shortcuts is slow as hell.
P.S. It would also be great if a-Shell pickFolder would accept a folder path as argument, so we would be able to pass that with the share menu inside the Files app.