Sunday, December 15, 2019

#1 2019-07-18 07:05:49 pm

JQ
Member
Registered: 2019-02-04
Posts: 37

DropBox Folder Actions - Move File

Hello,

I'm trying to write a script that I can turn into a folder action to move photos I receive on my MAC via DropBox.  I only want to move files taken on certain cameras, and I'm trying to get it to trigger anytime DropBox syncs and drops a file into the chosen folder. (When my inlaws drop a picture into our shared folder, I want to automatically move the file from that shared folder to my camera uploads folder. But I don't want it to touch photos that I'm sending to them that were taken on my iphone.)

This is what I have so far:

Applescript:


on adding folder items to this_folder after receiving these_items
   repeat with i from 1 to count of these_items
       set thephoto to item i of these_items
       set camera to do shell script "mdls " & quoted form of POSIX path of thephoto & " -name kMDItemAcquisitionModel"
       if camera contains "SM-G960W" or camera contains "Canon PowerShot ELPH 340 HS" then
           set filename to do shell script "basename " & quoted form of POSIX path of thephoto
           set theoldpath to quoted form of the POSIX path of thephoto
           set thenewpath to quoted form of ("/Users/user/Dropbox/Camera Uploads/" & filename)
           do shell script "mv " & theoldpath & " " & thenewpath
       end if
   end repeat
end adding folder items to

I've attached this folder action to the folder, but it only seems to trigger if I drop a file into the folder myself.  If DropBox puts a file into the folder it doesn't trigger. 

The default "add new item alert" folder action seems to trigger properly.  But I can't tell what's different from that script to the one I created.

Here is the default "add new item" script for reference:

Applescript:


property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
   try
       tell application "Finder"
           --get the name of the folder
           set the folder_name to the name of this_folder
       end tell
       
       -- find out how many new items have been placed in the folder
       set the item_count to the number of items in the added_items
       --create the alert string
       set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
       if the item_count is greater than 1 then
           set alert_message to alert_message & (the item_count as text) & " new items have "
       else
           set alert_message to alert_message & "One new item has "
       end if
       set alert_message to alert_message & "been placed in folder " & «data utxt201C» & the folder_name & «data utxt201D» & "."
       set the alert_message to (the alert_message & return & return & "Would you like to view the added items?")
       
       display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout
       set the user_choice to the button returned of the result
       
       if user_choice is "Yes" then
           tell application "Finder"
               --go to the desktop
               activate
               --open the folder
               open this_folder
               --select the items
               reveal the added_items
           end tell
       end if
   end try
end adding folder items to

Am I missing something in my script?

Browser: Safari 537.36
Operating System: macOS 10.14

Offline

 

#2 2019-07-18 09:50:53 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 802

Re: DropBox Folder Actions - Move File

Most probable, your problem is here:

Applescript:

do shell script "mv " & theoldpath & " " & thenewpath

when your rename one item (moving item is equivalent to renaming) inside HOT folder, HOT folder considers it as a new added item, so your on adding handler enters an endless cycle of calls

How to solve this problem: create another folder inside /Users/user/Dropbox (for example, /Users/user/Dropbox/myHotFolder) and make this folder HOT, not /Users/user/Dropbox. Then you can move files from this HOT folder to your folder /Users/user/Dropbox/Camera Uploads without problem. The most important is that: your destination folder must be outside the HOT folder.

Last edited by KniazidisR (2019-07-18 10:28:25 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#3 2019-07-19 07:27:55 am

JQ
Member
Registered: 2019-02-04
Posts: 37

Re: DropBox Folder Actions - Move File

The folder structure right now is like this:

Source Folder with Folder Action attached:

Users/user/Dropbox/Inlaw Photos

Destination folder:

Users/user/Dropbox/Camera Uploads

The hot folder and destination both exist in my Dropbox folder, but they are separate folders.

Offline

 

#4 2019-07-19 11:13:40 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 802

Re: DropBox Folder Actions - Move File

JQ wrote:

The hot folder and destination both exist in my Dropbox folder, but they are separate folders.



And? Works correct or no? Tell users to know if the problem is resolved, or not. I would not like to check myself. If not, then probably you forget to uncheck attached to Dropbox itself script. Right-click on Dropbox, go to Services-->Folder Actions Setup and uncheck your script above from Dropbox

Last edited by KniazidisR (2019-07-19 11:14:29 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#5 2019-07-19 05:13:50 pm

JQ
Member
Registered: 2019-02-04
Posts: 37

Re: DropBox Folder Actions - Move File

Sorry, I meant to say that the folder setup I listed is what I'm currently using and the files are not moving properly.

If Dropbox puts multiple files into the Users/user/Dropbox/Inlaw Photos folder the folder action only moves the first file it sees, but does not move the rest.

I'm currently using a variation of my original script as an automator quick action, but I'd love to figure out how to make it completely automatic.

This is the automator quick action script for anyone interested:

Applescript:


on run
   tell application "Finder" to set theSel to selection as alias list
   repeat with i from 1 to count of theSel
       set thephoto to POSIX path of item i of theSel
       set camera to do shell script "mdls " & quoted form of thephoto & " -name kMDItemAcquisitionModel"
       if camera contains "SM-G960W" or camera contains "Canon PowerShot ELPH 340 HS" then
           set filename to do shell script "basename " & quoted form of POSIX path of thephoto
           set theoldpath to quoted form of the POSIX path of thephoto
           set thenewpath to quoted form of ("/Users/Quimby/Dropbox/Camera Uploads/" & filename)
           do shell script "mv " & theoldpath & " " & thenewpath
       end if
   end repeat
end run

Offline

 

#6 2019-07-19 10:08:03 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 802

Re: DropBox Folder Actions - Move File

JQ wrote:

Sorry, I meant to say that the folder setup I listed is what I'm currently using and the files are not moving properly.

If Dropbox puts multiple files into the Users/user/Dropbox/Inlaw Photos folder the folder action only moves the first file it sees, but does not move the rest.



I'm not sure that the mv command can work with long names, with names containing non-ASCII characters and with names containing special characters. Why don't you throw away this do shell script and use the normal command without problems:

Applescript:

tell application "Finder" to move theFile to theFolder

I tested, the following script works fine:

Applescript:


on adding folder items to this_folder after receiving these_items
   set destinationFolder to (((path to home folder) as string) & "Dropbox:Camera Uploads") as alias
   repeat with i from 1 to count of these_items
       set thephoto to item i of these_items
       set camera to do shell script "mdls -name kMDItemAcquisitionModel " & quoted form of (POSIX path of thephoto)
       if camera contains "SM-G960W" or camera contains "Canon PowerShot ELPH 340 HS" then
           tell application "Finder" to move thephoto to destinationFolder
       end if
   end repeat
end adding folder items to

Last edited by KniazidisR (2019-07-20 11:55:40 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)