4

I am very new to AppleScript. I frequently share files from my local computer to Dropbox folder.

For example:

From: ~/Work/hello.txt
To: ~/Dropbox/zzz_ShareMe/hello.txt

cp ~/Work/hello.txt ~/Dropbox/zzz_ShareMe/ # or, just copy files from finder

I googled about AppleScript and came up with a workflow that copies ONLY ONE Item, But how can we select multiple files and copy them to Dropbox with an assigned hotkey (ctrl-alt-d)?

The contents of Run NSAppleScript is given below:

# Note: we should have ~/Dropbox/zzz_ShareMe folder
#
on alfred_script(q)
tell application "Finder"
    set source to (POSIX path of (the selection as alias))
    do shell script "cp -r" & space & quoted form of POSIX path of source & space & "~/Dropbox/zzz_ShareMe"

end tell
end alfred_script

I have shared my preliminary workflow here.

This workflow works for a single file. If I select a file in Finder, ctrl-alt-d copies the file to ~/Dropbox/zzz_ShareMe/, however, If I selected multiple files and used that hotkeys nothing is copied.

How can we fix that problem.

Help will be appreciated.

Related links:

http://www.packal.org/workflow/move
http://www.packal.org/workflow/copymove
https://www.alfredforum.com/topic/4195-move-selected-finder-item/

Update I tried another script, but it still fails.

Required task:

  1. select multiple files in Finder
  2. ctrl-alt-d
  3. it should copy all files to ~/Dropbox/zzz_ShareMe/.

The new script is given below:

on alfred_script(q) 

tell application "Finder"

set source to selection

end tell

if (count of source) is greater than 1 then

set fileList to every item of source

repeat with source in fileList

doMove(source, q)

end repeat

else

doMove(source, q)

end if

end alfred_script

on doMove(source, destination)

set source to (POSIX path of (the source as alias))

set destination to "~/Dropbox/zzz_ShareMe"


do shell script "cp -r" & space & quoted form of POSIX path of source & space & POSIX path of destination

end doMove
2

Ideally, you should have posted this question to the Alfred Forum. However, your problem is quite a simple one, so I'm happy to lend a hand here.

Essentially, your AppleScript for the single-file workflow inherently self-limits to processing a single file because the variable source is coerced into an alias, which is a unary data type (a single value). There is a class called alias list, which—as the name implies—is suitable for a list of aliases.

You've also mixed AppleScript with Bash script, which can be very useful in other situations, but is just a bit weird here since there's no reason to do so. So, I'll give you separate AppleScript and Bash solutions.

The Workflow

Create a workflow that compromises the following hotkey trigger connected to the Run Script (not Run NSAppleScript) action (see the Alfred Help pages for more information about these two actions, but generally speaking, you'll never need to use the Run NSAppleScript action):

Alfred Workflow

Double-click on the hotkey trigger to configure it. In the first tab (Hotkey Settings), I presume you know how to set the desired hotkey. However, also set the Argument field to Selection in macOS.

In the second tab (Related Apps), you can limit the action of the hotkey so that it operates only when Finder has focus. Drag Finder's app icon into the list box to do this (the easiest way is to trigger Alfred's search field, type in "Finder", and drag the icon from here to there).

Save the settings.

The Script

Double-click the Run Script action to bring up the editor pane. You can select the desired language in the first drop-down list: the two we're interested in for the purposes of this question are /bin/bash (or your equivalent list entry) and /usr/bin/osascript (AS) (or equivalent).

osascript is the engine that compiles and executes AppleScripts.

AS refers to AppleScript; JS refers to JavaScript.

Bash

If you elect to use Bash, select with input as argv. Delete any sample code that appears in the editing field, and enter this code:

cp "$@" ~/Dropbox/zzz_ShareMe

AppleScript

If you choose AppleScript, the input type doesn't matter here. Delete any sample code that appears in the editing field, and enter this code:

tell application "Finder" to reveal ¬
    (duplicate (selection as alias list) ¬
    to folder "zzz_ShareMe" ¬
    of folder "Dropbox" ¬
    of home)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.