Wednesday, December 7, 2022

Announcement

MacScripeter.net will transition to a new Discourse server soon. Watch this space for the date and time of the transition. Please expect the site to experience periods of outage during this transition. See this and this for more details.
  • Index
  •  » Automator
  •  » Create subfolder in selected folder not current directory

#1 2021-10-01 12:30:02 pm

jprokos
Member
Registered: 2019-09-02
Posts: 7

Create subfolder in selected folder not current directory

I want to make a new folder in the currently selected folder instead of the current directory using an automator Quick Action.

I have tried this but I am failing at the very beginning because for some reason "Get Value of Variable - Path" is giving me two paths instead of just the one that is selected in Finder. The second path is always ~/Desktop.


My idea was to get the path of the currently selected folder and then use that to run a shell script that uses that path with a "mkdir /Untitled Folder" command.

New Folder Here.workflow

Model: iMac
Browser: Safari 605.1.15
Operating System: macOS 10.14

Last edited by jprokos (2021-10-01 02:47:47 pm)

Offline

 

#2 2021-10-01 03:17:09 pm

jprokos
Member
Registered: 2019-09-02
Posts: 7

Re: Create subfolder in selected folder not current directory

I got it.

New Folder Here

Last edited by jprokos (2021-10-01 03:26:23 pm)

Offline

 

#3 2021-10-04 06:27:04 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 267

Re: Create subfolder in selected folder not current directory

I can't recall the exact details (and it's always awkward testing other people's services) but I think that two sets of data are being passed to the last action. In an earlier action, see if there is an 'ignore input' checkbox that you can check and try again.

I'll try and mimic this as best I can but I have an older OS so it might not be exactly the same.


Filed under: Automator, input

Offline

 

#4 2021-10-04 06:50:00 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 267

Re: Create subfolder in selected folder not current directory

Alternatively, how about letting automator make the new folder with its native actions?

Leave your 'service' setting as is but use these two actions instead:

Set Value of Variable
    Variable: Storage

New Folder
    Name: Entitled folder
    Where: Storage
    
    For the last action do these two things…
        Drag the 'Storage' variable onto the 'where' dropdown to set it
        Check the 'Ignore this action's input' option

The first action takes the selected folder and assigns its path to the variable 'Storage'.

The second action creates the new folder in that location. If you do not check 'ignore', then it will create folders recursively, which nobody wants.

Offline

 

#5 2021-10-05 10:37:47 am

jprokos
Member
Registered: 2019-09-02
Posts: 7

Re: Create subfolder in selected folder not current directory

That works too. Not sure which is faster or better but I like learning. For me the method I used is clear to read and decipher whereas yours is not, but I can never understand how Automator variables work and avoid them... like a black hole.

I also don't understand why "ignore this action's input" doesn't ignore "Set Value of Variable" the first time.

Offline

 

#6 2021-10-05 05:47:31 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 267

Re: Create subfolder in selected folder not current directory

I share those frustrations with figuring out how automator works under the hood, and for years and years I too avoided using variables. A while ago I managed to do something using one so now I'll use them occasionally. Part of the problem for me is that I always feel uncomfortable with 'run applescript/shell' in automator when I can't know exactly what file references are being passed on. I really wish that Apple had made an easy method of showing the user what the inputs and outputs are. What helped me is that I managed to get a 'run applescript' action throw up a dialogue displaying what the input was and how it basically stuck two unrelated file references into a single output and how that changed when I checked ignore.

How automator seems to work is that each action automatically passes whatever it did on to the next (unless you 'ignore'). And visually, you can see that with the connection between actions, which disappear when you check ignore. So I think of it as there being both explicit (Storage variable) and implicit (current folder) references being passed on. Ignore stops the implicit one. As an aside, Apple did a poor job with this in the 'dark' version. In the normal display, there is a directional aspect to the connection.

So by checking ignore in my workflow, nothing is automatically passed between the 'set value' and 'new folder' actions. Instead, it only reads Storage, whose value it puts in the 'where' field. If you didn't check ignore, then it would do something weird… it would take both the current folder and the new folder and recursively re-create pairs of folders. I get 20 of each when I run it and then it stops itself (thankfully). Weirder, it actually duplicates the current folder (including its other contents) so as my test folder also contains a word doc, that file was also duplicated 20 times.


Filed under: Automator, variable

Offline

 

#7 2022-06-17 01:31:54 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 133

Re: Create subfolder in selected folder not current directory

There's nothing unfathomable about Automator and its actions, although Apple had indeed, years to complete the detailed documentation. You have to do a little, that is, to grasp the concepts of a variable and an object reference which are the full analogies of the notions that are core to the AppleScript scripting language. Automator actions are visualized snippets of AppleScript statements programmed in the Obj-C language.

In Automator, there're three types of objects passed between actions: text, alias and POSIX path.

Text is the result of any action that returns plain text (file-names, math operations and every object that can be converted to text).

Alias is the most common class utilized by Automator to handle requests in-between (action A<|>action B) when manipulation with files is involved. It's used to refer to various objects of the file system (or, in programmer parlance, to form (an) object reference(s)) It's effectively, the class of AppleScript alias in the form of alias "Macintosh HD:Users:john:Documents:My_Report.txt" with one small caveat that since files usually are processed in groups their standard textual representation is that of a list. It means that the output of an Automator action typically is an alias list (an AppleScript class too) even if it returns only one file or folder.
Hence, the final form that such an action always outputs is {alias "Macintosh HD:Users:john:Documents:My_Report.txt"}. If you use the Run AppleScript action and the output of the previous action is {alias "Macintosh HD:Users:john:Documents:My_Report.txt"} then to get rid of the braces you need to pass the statement get the first item of input inside on run{input, parameters}...end run and if the list consists of an array of items then you need to process them one by one (loop through the list to apply commands to each of its members).

POSIX path is a standard Unix path as in /Users/john/Documents/My_Report.txt. It can be passed either as text or a list, but this time it's a Unix list as opposed to AppleScript's one. A Unix list is of the form "'/Folder/file.txt' '/AnotherFolder/another_file.txt' ... 'anything'" (the members that may be single-quoted are delimited by spaces) and so on. Usually, it's the result of a shell script within the action "Run Shell Script" and if you pass it as arguments by selecting the corresponding option in Run Shell Script then this action immediately prints out the multi-line template

for f in "$@"
do
echo "$f"
done



, where "$@" stores the Unix list as its input passed from the previous action (or piped from the previous action into the current one).

Automator has a dedicated variable section called "Variables" that is none other than a selection of AppleScript Standard Additions commands' graphical counterparts and variables of certain types named by the category of a value they hold (Path, Destination Path, Text, Storage etc). They're split between 2 types: one type that has the variables shown as coloured gear icons represents persistent values that are stored in these variables beyond user's capacity to change them (current month, Applications, host name, user name, e-mail etc). The other type are those that can be changed by the user with the icon "V" (stands for "Variable", i.e., an actual settable variable). The "V" actions are essentially the same actions that are available under "Actions": Set Value Of Variable and Get Value Of Variable. The difference between the variables of these 2 sections is that to set a variable chosen in "Actions" such an action must be preceded by another action, and the actions of the "Variables" section are specialized variables that offer a more flexible approach: they can generate different kinds of input on the fly without additional efforts, store scripts (AppleScript, shell script, javascript), concatenate (combine) the output of one another, they can be dragged to fields in other actions. They're thought out to enhance, embellish the workflow since everything is, basically, "actions". E.g., a long AppleScript script action can produce the result that is ready to be literally "inserted" in a text field (i.e., "name") of any action that has the field and accepts text.

With that in mind, let's get down to this example and see where it falters (and, indeed, it does):

Set Value of Variable
    Variable: Storage

New Folder
    Name: Entitled folder
    Where: Storage
   
    For the last action do these two things…
        Drag the 'Storage' variable onto the 'where' dropdown to set it
        Check the 'Ignore this action's input' option



I assume that by "Storage" you mean the variable Storage from "Variables". If you do, then this variable stores any information (a date, path, word) in a textual representation. If you want to set it to a reference to a choosable folder, and if you use "Ask for Finder item" then you don't need Storage at all. In any case, Storage requires input and it's usage is identical to that of "Set Value Of Variable". What do you want to set it to?

In the case of Path, this variable returns a Posix path to the file/folder you select as text. When you place it after "Ask for Finder item" it simply combines the reference to the folder you choose with the former with the designated path returned by the latter into a list which isn't what you need.

Do you want to create a new folder with your specified name? You can do that with the "New Folder" action ("Actions").
What do you mean by currently selected? Is it highlighted in Finder, so you want to create a child folder inside the highlighted folder?

If the latter then you need as few as 3 actions:

1. Get Selected Finder Items
2. Set Value Of Variable
3. Make New Folder.

Set the quick action to receive folders in Finder. Then choose "Variable..." from the popup menu of "Make New Folder". By default, it will be Path, the same Path (see above) that sits in the section "Variables". Once you choose it, the "Set Value of Variable" action will update its value to Path. Next, in "Make New Folder" check the boxes "Ignore input" and "Show this action when the workflow runs": the latter is needed so you can type a name of your folder. Save the Quick Action. That's it.

On top of that, this workflow is identical to the command "New Folder" of the Finder menu bar or from the Finder window toolbar: when you highlight a folder the command creates the subfolder automatically.


roRHVc0.png

Examples of using Automator variables are shown in this neat video:

https://www.youtube.com/watch?v=-Gpp_hYQOq8

Last edited by scrutinizer82 (2022-10-29 09:59:11 am)


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6


Filed under: folder, Finder, Automator, Make

Offline

 
  • Index
  •  » Automator
  •  » Create subfolder in selected folder not current directory

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)