Jump to content

Move selected file to favourite folder


noisyneil

Recommended Posts

Hi. 👋🏼

 

I'd like to build a simple workflow that will move the Finder selection to a subfolder of a single parent directory, using AppleScript. Example:

 

1. I select my file(s) and invoke the workflow.

2. Alfred displays the parent directory in its interface.

3. I drill down to find the desired subfolder.

4. I hit enter and it moves the file there. 

 

This workflow almost does it, but doesn't support subfolders. If you type the name of the folder you want to send the file to and hit enter, it moves it there, but if you try to drill down with the right arrow key, hitting enter opens that folder in a new Finder window instead. Could it perhaps be modified to do what I'd like it to? I like that it uses AppleScript, because that means it has the potential to allow Finder's undo capability, although that doesn't seem to work with the workflow I posted above for some reason.

 

Many thanks for any help,

 

Neil

Edited by noisyneil
Link to comment
2 hours ago, noisyneil said:

I'd like to build a simple workflow that will move the Finder selection to a subfolder of a single parent directory

 

Can you be more precise? Like, is it always the same parent directory, or do you mean the parent directory of the files you're moving?

 

Describe the task as if you were explaining how to move the files to someone who's got to do it for you while you're on holiday.

Link to comment
2 hours ago, noisyneil said:

but if you try to drill down with the right arrow key

 

That's because doing that exits the workflow. It uses a regular File Filter: there is no drilling down, just a flat list of results.

 

If you want to be able to use Alfred's full navigation capabilities, you need to re-implement the move as a File Action. Then you can call it on any folder shown anywhere within Alfred at any time. (Or a folder in any other application if using a Hotkey.)

Edited by deanishe
Link to comment
1 hour ago, deanishe said:

 

Can you be more precise? Like, is it always the same parent directory, or do you mean the parent directory of the files you're moving?

 

Describe the task as if you were explaining how to move the files to someone who's got to do it for you while you're on holiday.

I'll do my best:

 

I keep a bunch of audio/video projects on my internal SSD while they're in progress. Once they're finished (or put on hold), I want to archive them to an external drive, categorised according to client, project type, genre, level of completion, etc. 

 

For this purpose, I have an existing folder on my external drive called "Projects"; within is the respective hierarchy of sub-folders. 

 

I want to select a group of files and/or folders on my internal drive, invoke a workflow and immediately see this hierarchy of subfolders within Alfred's browser. Using the arrow keys, I would then drill down to the correct destination for this particular group of files and hit Enter. The selected files would then be moved, ideally with the possibility of undoing the move that AppleScript has the potential to allow, should I accidentally send something to the wrong destination folder. 

 

In short, for this purpose the parent folder would be static.

 

1 hour ago, deanishe said:

 

That's because doing that exits the workflow. It uses a regular File Filter: there is no drilling down, just a flat list of results.

 

If you want to be able to use Alfred's full navigation capabilities, you need to re-implement the move as a File Action. Then you can call it on any folder shown anywhere within Alfred at any time. (Or a folder in any other application if using a Hotkey.)

 

I'm not sure how to do this. Any advice would be much appreciated. I've had Alfred for years but never made any workflows beyond the most simple you can imagine. 

Link to comment

Ok, so I think I've figured out how to make the AppleScript into a file action! 

 

I can now manually navigate to the destination folder, hit ctrl, select my file action and hit enter. It works! For some reason, undoing the move isn't possible though. 😢

 

image.png.37b49aa86db9c94627c218cda42f4778.png

image.png.00d7b6bc9d582454dc801c0a524b4641.png

 

At the moment, this takes longer than manually dragging and dropping. For it to be the full-blooded time saver that I'm hoping for I'd like to reduce the number of steps, if possible. 

 

I'd like to hit a hotkey which takes me directly to the parent directory in Alfred's browser (so it displays the first level of subfolders below the parent). Then I'd drill down to the desired destination for the file(s), hit enter, and they move. 

So far, I have a hotkey that gets Alfred to navigate to the parent folder. Great!

image.png.ea76e2d70ac66f965835447f5f282a57.png

 

How do I get the script to run (as a file action or otherwise) upon hitting Enter?

 

 

Edited by noisyneil
Link to comment
1 hour ago, noisyneil said:

For some reason, undoing the move isn't possible though.

 

Undo only works if you tell Finder to move the files itself, not if you tell it to run a shell script. How is Finder supposed to know how to undo that?

 

2 hours ago, noisyneil said:

How do I get the script to run (as a file action or otherwise) upon hitting Enter?

 

You can't directly plug Alfred's navigation mode into your own code. You have to go via the File Actions menu.

Link to comment
23 minutes ago, deanishe said:

Undo only works if you tell Finder to move the files itself, not if you tell it to run a shell script. How is Finder supposed to know how to undo that?

I have another AppleScript that moves files, which I trigger via Keyboard Maestro and undo works fine. This must be, as you say, because it isn't using a shell script:

 

Pasted_Image_26_11_2021__15_01.png.be2f178013592b4468035ab0c799c888.png


Unfortunately my scripting knowledge is practically nil, so I'm not sure how to replace the shell script part with pure AppleScript language, but it seems that could solve the problem. 

 

32 minutes ago, deanishe said:

You can't directly plug Alfred's navigation mode into your own code. You have to go via the File Actions menu.


Seems that I've misunderstood a core concept, haven't I! Ok, how about a hotkey to perform a file action on Alfred's browser selection. That would be preferable than having to manually select the file action from the menu, if it's possible. 

Link to comment
2 minutes ago, noisyneil said:

Unfortunately my scripting knowledge is practically nil, so I'm not sure how to replace the shell script part with pure AppleScript language, but it seems that could solve the problem.

 

Replace FileDest with the path you get from Alfred.

 

3 minutes ago, noisyneil said:

a hotkey to perform a file action on Alfred's browser selection.

 

Any Hotkey with "Selection in macOS" also works in Alfred.

Link to comment
28 minutes ago, deanishe said:

Replace FileDest with the path you get from Alfred.

 

I don't know what you mean. Are you saying I can use Alfred to get the destination folder path and then use Keyboard Maestro to run the "Move File" script using that path from the clipboard? If so, how do I get the path?

 

28 minutes ago, deanishe said:

Any Hotkey with "Selection in macOS" also works in Alfred.

Again, I don't know what you mean. I'm asking if, once you have a destination folder selected in Alfred's browser, it's possible to hit a hotkey and run the script as a file action. 

Edited by noisyneil
Link to comment
26 minutes ago, noisyneil said:

Are you saying I can use Alfred to get the destination folder path and then use Keyboard Maestro to run the "Move File" script using that path from the clipboard?

 

No. Read the code you posted. The only Keyboard Maestro-specific thing about it is the way you get the destination directory.

 

28 minutes ago, noisyneil said:

Again, I don't know what you mean.

 

If you add a Hotkey to a workflow with "Selection in macOS" as the Argument, it will pass the path of the selected file to the next action. It works in Alfred just as well as it works in Finder.

Link to comment

Sorry if this is at all frustrating but you're dealing with a workflow noob here. Your answers, while I'm sure they're correct, are very brief and I'm struggling to make sense of them. 

I tried my hardest to think it through and came up with this but it doesn't work:

image.png.753bccea8278da1c66e61d493088809c.png



So I should set up a hotkey trigger and trigger the script (once I've figured it out) directly? I thought you said it would only work as a file action and I can't see a way to trigger a file action from a hotkey. 

 

Edited by noisyneil
Link to comment
9 hours ago, noisyneil said:

Your answers, while I'm sure they're correct, are very brief and I'm struggling to make sense of them.

 

That's entirely on purpose, tbh. You might not know what you're doing, but you've certainly got the right attitude and approach. So I'm trying to point you in the right direction rather than spell things out for you.

 

9 hours ago, noisyneil said:

I tried my hardest to think it through and came up with this but it doesn't work

 

You’re pretty close, though. I’d encourage you to look more closely at what you’re doing with the destination variable. In the second “paragraph”, you’re still trying to use FileDest, which you’ve removed from the script.

 

Anyway, here's a working version of the script (I think: I haven't tested it in a workflow). Note, this won't work with Run NSAppleScript. You shouldn't use that. Instead, use a regular Run Script action with Language = "/usr/bin/osascript (AppleScript)". It works a lot better than Run NSAppleScript.

 

on run (argv)
	tell application "Finder"
		-- set destination from first command-line argument
		set _destination to (first item of argv) as alias

		set _selected to selection as alias list
		move _selected to _destination
	end tell
end run

 

Edited by deanishe
Link to comment
9 hours ago, deanishe said:

you've certainly got the right attitude and approach. So I'm trying to point you in the right direction rather than spell things out for you.

I appreciate it. Probably the best way for me to learn, rather than be spoon-fed. As long as it's not tiresome for you!
 

9 hours ago, deanishe said:

In the second “paragraph”, you’re still trying to use FileDest, which you’ve removed from the script.

🤦🏻‍♂️  D'oh!
 

9 hours ago, deanishe said:

here's a working version of the script

Thanks for taking the time to do that! 

 

So here's where I'm at, currently:

I can now hit a hotkey and navigate straight to the parent directory within Alfred's browser. From there I can use the arrow keys to drill down into the desired destination folder. So far so good!

 

I made the script you sent me into into a file action, but when I select and trigger it, nothing happens. Changing the errant 'FileDest' to 'destination' in the previous AppleScript version doesn't work either. 

 

I tried assigning the scripts directly to a hotkey, but again nothing happens when I trigger them (while the destination folder is selected in Alfred's browser). 

 

I suppose the first thing is to get the script working. Once that's done, it needs to be triggered. The ideal scenario would be that you hit enter when the folder is selected. As I understand it, that's not possible with Alfred, so I wonder if this might be a way of doing it?:

 

Could we add a line to the script that tells it to wait until the enter key is depressed before running? That way it's already been triggered by the same hotkey that navigates to the parent directory but doesn't do anything until I've confirmed the selection. Just trying to think laterally here. Thanks again for your help so far! 

 

 

 

 

 

 

Edited by noisyneil
Link to comment
30 minutes ago, noisyneil said:

but doesn't do anything until I've confirmed the selection

 

I'm not sure I've understood you correctly here. It sounds like you need to pass the selection to an already-running script. How do you plan to do that?

 

32 minutes ago, noisyneil said:

but again nothing happens when I trigger them

 

Can't tell you anything based on a description like that of a problem with a workflow I've never seen.

 

Upload your workflow somewhere (iCloud? Dropbox?) and post a link.

Link to comment
9 minutes ago, deanishe said:

I'm not sure I've understood you correctly here. It sounds like you need to pass the selection to an already-running script. How do you plan to do that?

 

I just did some research and found that AppleScript can't respond to user input, so there's no point trying to explain my (probably flawed) logic.

 

I am surprised that Alfred doesn't have a 'pause until' module, which could be used to trigger subsequent actions, but I think that's just me with my KM head on. 

 

11 minutes ago, deanishe said:

Upload your workflow

 

There's not much to it at the moment as I can't get the two actions (browse and script) to work together as one. It's just a browse command and two options for trying to trigger the script, neither of which work. 

image.png.125340876b113cded424a8541871009b.png

Link to comment
19 minutes ago, noisyneil said:

neither of which work

 

That's what we're trying to change, isn't it?

 

image.png.125340876b113cded424a8541871009b.png

 

Please don't post screenshots of workflows. I mean, what is anybody supposed to do with that? Upload the actual workflow somewhere and post a link, so we can see what you're actually talking about.

Link to comment
7 minutes ago, noisyneil said:

you'd know what's what

 

But I have no idea what you've done, do I?

 

Right, there are two problems. First, my script has an error (because I didn't test it in Alfred). It should be:

on run (argv)
	tell application "Finder"
		-- set destination from first command-line argument
		set _destination to POSIX file (first item of argv) as alias

		set _selected to selection as alias list
		move _selected to _destination
	end tell
end run

 

(There was a POSIX file missing).

 

Secondly, the Hotkey you want to use to select the destination is set to only work in Finder, so of course nothing happens when you try to use it in Alfred.

Link to comment

I just tried it and it's allllmost perfect. The only issue is that it copies, rather than moves the file, which I suspect is because the destination folder is on a different volume. I know it's possible to move between volumes, as the other 'move file' script I use with KM does it nicely. One little tweak and it's a home run! 

21 hours ago, noisyneil said:

Pasted_Image_26_11_2021__15_01.png.be2f178013592b4468035ab0c799c888.png

 

Link to comment
22 minutes ago, deanishe said:

Delete the originals after the copy?

That isn't necessary in the other AppleScript code, so I'm wondering what's different about the one we're now using? Given that it's two steps (copy then delete), it might mess up Finder's undo-ability, no?

Regardless, I don't know how to do that, as usual. 🥴

 

Edited by noisyneil
Link to comment
8 hours ago, noisyneil said:

so I'm wondering what's different about the one we're now using?

 

Not a clue. It's exactly the same code, as far as I can tell.

 

8 hours ago, noisyneil said:

it might mess up Finder's undo-ability, no?

 

Not if you also tell Finder to do the deleting. There'll be two operations on the undo stack, instead of one, but I wouldn't call that messed up. It's arguably better that way because the first undo would restore the originals from the trash, which will be a lot quicker than copying them back over from another volume.

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