Jump to content

New Path — Make new files and folders in the current Finder location


Recommended Posts

2 hours ago, vitor said:

Adding that to the Workflow wouldn’t save any work.

I would personally used it for creating multiple folders at once. Currently I'm using this command in Terminal to do so: 

mkdir 'folder01' 'folder02' 'folder03' - but being able to execute it within Alfred would be great.

Link to comment

@Phaks On the Run Script, edit the touch "${file_location}" line (near the end) to be mkdir "${file_location}", and delete the [[ -z "${file_extension}" ]] && file_extension=".txt" line (a few above). That’ll do what you want, and it’s the extent to which I will support that feature.

Link to comment

@Phaks I use the following code:

 

dir_path="$(osascript -l JavaScript -e '
  const frontmost_app_name = Application("System Events").applicationProcesses.where({ frontmost: true }).name()[0]
  if (frontmost_app_name === "Finder") {
    decodeURIComponent(Application("Finder").insertionLocation().url()).slice(7).slice(0, -1)
  } else if(frontmost_app_name === "Path Finder") {
    decodeURIComponent(Application("Path Finder").finderWindows[0].target.url()).slice(7).slice(0, -1)
  } else {
    decodeURIComponent(Application("Finder").desktop.url()).slice(7).slice(0, -1)
  }
')"
IFS=$'/\n'
for dir in ${@}; do
  dir_location="${dir_path}/${dir}"
  mkdir "${dir_location}"
  if [[ "${open_dirs}" == 'yes' ]]; then open "${dir_location}"; fi
done

 

Note that I also changed the variable names in the Arg and vars utilities from open_files to open_dirs.

 

Will later look into enabling syntax that directories can be created using native mkdir syntax such as (nd is the Alfred command for creating the new directories):

 

nd Dir1 'Dir 2' "Dir 3" Dir{4,5,6} Dir{7..100}

 

to create below directories in current foremost directory:

 

Dir1

Dir 2

Dir 3

Dir 4

Dir 5

Dir 6

Dir 7

...

Dir 100

Link to comment

1. Copy workflow NewFile and rename copy to NewDir

2. Change keywords nf to nd and nfo to ndo in NewDir

3. Change variable open_file to open_dirs in the two Args and vars utilities.

4. Edit the Run script action, change the with input as argv to with input as {query}, and running instances should be Sequentially, and then replace the Script with below:

 

# -----------------------------------------------------------------------------

# CREDITS:
# - Step 1 and partially Step 3 is based on Vítor Galvão:s NewFile workflow

# NOTES:
# - This is just a simple wrapper around the built-in macOS bash-shell and mkdir command.
# - To use spaces in directory names, use quotes (single and double work): "Dir 1", 'Dir 1', "Dir 1-"{1..3}, 'Dir 2-'{1..3} etc.
# - This uses spaces as argument delimiters, change Step 2 below if you want to use another delimiter

# USE EXAMPLES:
# - nd dir1
# - nd dir1 dir2
# - nd 'dir 1'
# - nd "dir 1"
# - nd 'dir 1' 'dir 2'
# - nd 'dir '{1..10}
# - nd "dir "{1..10}
# - nd 'dir 1-'{1..10} 'dir 2-'{1..10}
# - nd "dir 1-"{1..10} "dir 2-"{1..10}
# - nd 'dir 1' 'dir 2' 'dir 3' 'dir {4,5,10}' 'dir '{10..100}
# - nd 'dir 1' 'dir 2' 'dir 3-'{1..3} 'dir 4-'{1..3}
# - nd dir{1..10}/subdir{1..10}
# - nd 'dir '{1..10}/'subdir '{1..10}

# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

# Step 1. Set pwd/dir_path to that of frontmost app

dir_path="$(osascript -l JavaScript -e '
  const frontmost_app_name = Application("System Events").applicationProcesses.where({ frontmost: true }).name()[0]

  if (frontmost_app_name === "Finder") {
    decodeURIComponent(Application("Finder").insertionLocation().url()).slice(7).slice(0, -1)
  } else if(frontmost_app_name === "Path Finder") {
    decodeURIComponent(Application("Path Finder").finderWindows[0].target.url()).slice(7).slice(0, -1)
  } else {
    decodeURIComponent(Application("Finder").desktop.url()).slice(7).slice(0, -1)
  }
')"

# -----------------------------------------------------------------------------

# Step 2. Set spaces as delimiters for arguments
IFS=$' '

# -----------------------------------------------------------------------------

# Step 3. for each arg, create dir(s), and if ndo command: open them afterwards
for dir in {query}; do

  # set new dir loc according to frontmost app
  new_dir_loc="${dir_path}/${dir}"

  # create new dir(s) according to arguments
  mkdir -p "${new_dir_loc}"

  # if using ndo command, then open newly created dir(s) afterwards
  if [[ "${open_dirs}" == 'yes' ]];
    then open "${new_dir_loc}";
  fi
done

# -----------------------------------------------------------------------------

 

The above works for simple Alfred commands such as:

 

nd dir to create dir named dir in foremost dir.

nd dir{1,2,3} to create dirs named dir1,dir2,dir3 in foremost dir.

nd dir{1..3} to create dirs named dir1,dir2,dir3 in foremost dir.

nd dir{1..3} dir{5..8} to create dirs named dir1,dir2,dir3,dir5,dir6,dir7,dir8 in foremost dir.

nd dir{1..3}/dir{1..3} to create dirs dir1, dir2, dir3, dir1/dir1, dir1/dir2, dir1/dir3, dir2/dir1, dir2/dir2 etc. starting from foremost dir.

etc.

Edited by mjp
Link to comment
18 minutes ago, mjp said:

Copy workflow NewFile and rename copy to NewDir

 

If I were you, I'd release that workflow (and create a new thread for it). It looks really useful, and a lot of people don't really have the skills to easily modify an existing workflow. Your post is also likely not to age well, as the NewFile workflow might look very different in five years' time (when people will still be finding your post and wanting to have the workflow).

Link to comment

@deanishe: I will consider that. Haven't read about best practice regarding releasing workflows yet, as I just spent some minutes hacking it together. Will read up on it and release it. But it feels somewhat banal as I just hacked it together borrowing the most interesting parts from @vitor's NewFile. I feel it (or a better version of it) would do better as part of @vitor:s already excellent set of workflows which has more audience attraction and better chance of reaching those users that might have use for it. But if @vitor doesn't mind, I can look into releasing it as a separate one. Will give credit to @vitor of course.

Link to comment
4 hours ago, mjp said:

using native mkdir syntax

 

The examples you posted are handled by the shell, not mkdir. That is to say, “mkdir syntax” doesn’t exist.


But what you want is pretty easy to implement: change the Run Script from with input as argv to with input as {query} and just have it mkdir {query}. However, I’ll recommend against that because it means you then have to write all your names with a shell’s syntax and care. By that point, might as well use RunCommand; doing it in NewFile would be duplication of functionality.


I still don’t see much point in it, but if someone wants it enough to modify it to that extent, I’ll consider it. Though for file creation I settled on / as the separator because it isn’t used in names, but in directories that could be valid for subdirectories. I may change the separator to |. Though I probably still won’t add the shell creation features because I don’t want people to shoot themselves in the foot by mistake; complex behaviour should be handled by RunCommand.


I’ll probably also change the Workflow’s name to NewPath, though you don’t have to worry about that as OneUpdater will take care of it.

Link to comment
  • vitor changed the title to NewPath — Creates new files or directories in the current Finder directory
  • 2 weeks later...

I added a hotkey for generating an often used txt-file of mine (see picture).

 

Is it possibility to add the hotkey so, that it is not necessary to press "enter", after triggering?

 

Greetz Chris

Bildschirmfoto 2021-05-09 um 04.09.52.png

Link to comment
  • 1 month later...
7 minutes ago, Pseud0nymer said:

Sorry to find that this workflow failed to create .xlsx files. Did I make any mistakes when performing the action?

Thank you!

CS 2021-07-04 at 09.34.37.png

CS 2021-07-04 at 09.34.05.png

 

What does the dialog say? Very few forum members understand Chinese.

Link to comment

The Workflow just creates empty files with the appropriate extension. Some file types, even when “empty”, have a specific structure. That’s not something I want to support, I don’t intend to ship a file of each with the Workflow for a narrow use case.

Link to comment
3 hours ago, vitor said:

I don’t intend to ship a file of each with the Workflow for a narrow use case.

 

How about creating a templates directory in the workflow’s data directory where users can put their own empty (or not) files? Then if there’s a file in there with the same extension as the new file the user’s creating, copy that instead of just touching a file?

Link to comment
On 7/4/2021 at 12:22 PM, deanishe said:

How about creating a templates directory in the workflow’s data directory where users can put their own

 

Nice idea. Won’t commit to it just yet because I’m having some busy days, but I’m not at all opposed to it.

 

On 7/4/2021 at 12:22 PM, deanishe said:

empty (or not) files

 

For the “or not” case, there’s TemplatesManager.

Link to comment
  • 2 weeks later...
  • 1 year later...
  • vitor changed the title to NewPath — Creates new files or folders in the current Finder folder
  • vitor changed the title to NewPath — Make new files and folders in the current Finder location
  • 3 weeks later...

@vitor I just found this great workflow. One thing I am running into. When I create a new path with your workflow and later try to use my universal action hotkey it perceives that folder as text and only provides me with text actions. Any folder created in the finder itself that is sent to alfred with the universal action hotkey is perceived as a file and provides more options. If i go into the finder and rename the folder that was created with your workflow and then use the universal action hotkey alfred presents the file options. Any reason why alfred is thinking folders created with your workflow are just text? Can  you recreate?

 

I'm on 12.6.

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...