Jump to content

NewPath — Creates new files or directories in the current Finder directory


Recommended Posts

23 hours ago, Phaks said:

Is there a way to create a folder (or multiple folders at once)?

 

Yes, that’s pretty easy to do but I don’t see the point of adding that to this Workflow when you can just ⌘⇧N and start typing to make a new directory. Adding that to the Workflow wouldn’t save any work.

Link to post
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 post

@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 post

@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 post

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 post
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 post

@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 post
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 post
  • vitor changed the title to NewPath — Creates new files or directories in the current Finder directory

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...