Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

moving folders containing "name" to another folder

Hi all,


I'm trying to figure out the easiest way to create an applescript that can be run periodically as an application to do some maintenance with folders. For example:

there's 2 folders on the desktop. one is named "George Project" and the other named "Rocco project". I need any folders, and their respective sub-folders, containing George, to be moved to a george folder on a netowrk drive (/Volumes/AMT/George), and the same thing with any folders containing Rocco, etc. All of these named folders would only be in 2 locations, so I'd like the script to check both locations for any folders containing these names and move them to the right place on the network.


Thanks so much,


Rocco

MacBook Pro, OS X Yosemite (10.10.5)

Posted on Nov 20, 2015 2:01 PM

Reply
13 replies

Nov 27, 2015 2:27 PM in response to Roccoiphone

I found this script and modified it, but it hasn't actually moved any files...wondering what's wrong?


global theWatchedFolder

set theWatchedFolder to choose folder

on idle

tell application "Finder"

set theDetectedItems to every item of theWatchedFolder whose name contains "rocco"

repeat with aDetectedItem in theDetectedItems


moveaDetectedItemto"/Users/Rocco/Test"

end repeat

end tell

return 1

end idle

Nov 27, 2015 3:59 PM in response to Roccoiphone

Modify the following as appropriate. Any error number 0 indicates that there was no error moving the document to the destination location.


set target_path to "/Volumes/Storage/Test/"

global theDetectedItems

set theDetectedItems to {}


set theWatchedFolder to choose folder


tell application "Finder"

activate

set theDetectedItems to (every item of theWatchedFolder whose name contains "rocco") as alias list

try

repeat with aDetectedItem in theDetectedItems

-- duplicate aDetectedItem to (target_path as POSIX file) with replacing and exact copy

moveaDetectedItemto (target_path as POSIX file) with replacing

end repeat

on error errmsg number errnbr

display dialog "[ " & errnbr & " ] " & errmsg

quit

end try

end tell

return

Nov 28, 2015 5:06 AM in response to Roccoiphone

I was not receiving any -10000 error when I tested the code on El Capitan (10.11.1), and Yosemite (10.10.5). Would not have posted it.


Post the code back here that causes the above error. Use the advanced editor feature here before you copy/paste the code from Script Editor. Then select all of the partially visible code, and choose Style : Paragraph. That will show all of your pasted code.

Nov 28, 2015 8:49 AM in response to VikingOSX

Hi, I made a few changes, and now the error is that it can't make the items into an alias list. see below:


set theWatchedFolder to "/Users/Rocco/source"

set target_path to "/Users/Rocco/destination"

global theDetectedItems

set theDetectedItems to {}



tell application "Finder"


activate

set theDetectedItems to (every item of theWatchedFolder whose name contains "rocco") as alias list

try

repeat with aDetectedItem in theDetectedItems


-- duplicate aDetectedItem to (target_path as POSIX file) with replacing and exact copy


moveaDetectedItemto (target_path as POSIX file) with replacing

end repeat

on error errmsg number errnbr

display dialog "[ " & errnbr & " ] " & errmsg


quit

end try

end tell

Nov 29, 2015 1:42 PM in response to Roccoiphone

When you were using the choose folder approach, it returned an Apple HFS filesystem designation as an alias — not a POSIX path as you have now designated with "/Users/rocco/source", and it is now that POSIX path that is breaking the alias list generation.


Use the following to generate your alias list by switching to the proper HFS designation that must be used with the Finder. The log statement shows you what the HFS syntax should look like. You use as alias because the folder exists already. The as POSIX file clause changes a POSIX path into an HFS style format.


set theWatchedFolder to ((path to home folder) as text) & "source:" as alias

logtheWatchedFolder

Nov 28, 2015 7:03 PM in response to Roccoiphone

In the Script Editor File menu, there is Open Dictionary… . Locate System Events, and select it. Once you have the scripting dictionary open for System Events, Select the Disk-Folder-File Suite category, and then application. There will be a list of location properties that you can use with the path statement. These are available for assignment directly in AppleScript without needing the Finder or System Events. The trailing colons signify a folder.


For example, let's say you want to set the target_path to a Test folder located on a mounted volume Storage:

set x to ((path to startup disk) as text) & "Volumes:" & "Storage:" & "Test:") as alias


All that just for /Volumes/Storage/Text in POSIX parlance, and the Storage icon mounted on your Desktop.

User uploaded file

Nov 29, 2015 1:53 PM in response to Roccoiphone

When I looked at this script further, I substituted the correct directories, but still got an error about making the directory into a posix file. When I removed the choose folder prompt, and the posix file parts of the code, everything worked as expected. Is there a special reason why the posix file part of the script was there? Is there a way for me to write out set theWatchedFolder to "/Volumes/Storage/Folder" or "/Users/username/Folder" in this format? Also, is there a way for me to set multiple watched folders and target folders to look for different characters in each folder? Or do I need to make a script for each person I'm looking for? (Rocco, User B, etc.) If I need to make different script files for each destination folder, can I have all of these files run simultaneously, as an app?

Here's the script I have now. These source and destination folders are examples on my local hard drive, but I will replace them with the actual folders on my work machine when I have access to it tomorrow.


set theWatchedFolder to ((path to home folder) as text) & "source:" as alias

logtheWatchedFolder


set target_path to ((path to home folder) as text) & "destination:" as alias

logtarget_path

global theDetectedItems

set theDetectedItems to {}



tell application "Finder"

set theDetectedItems to (every item of theWatchedFolder whose name contains "rocco") as alias list

try

repeat with aDetectedItem in theDetectedItems


-- duplicate aDetectedItem to (target_path with replacing and exact copy


moveaDetectedItemto (target_path) with replacing

end repeat

on error errmsg number errnbr

display dialog "[ " & errnbr & " ] " & errmsg


quit

end try

end tell

Thanks so much, sorry for all of the questions. I also tried to look at the dictionary in script editor but it didn't make sense to me. I did understand your example though, thanks for that!


Rocco

Nov 29, 2015 3:49 PM in response to Roccoiphone

When I looked at this script further, I substituted the correct directories, but still got an error about making the directory into a posix file. When I removed the choose folder prompt, and the posix file parts of the code, everything worked as expected. Is there a special reason why the posix file part of the script was there?


When you changed the target_path from the POSIX syntax into an HFS path, then as POSIX file is unnecessary. You use as POSIX file only when you want to convert a POSIX path into an HFS path.


Is there a way for me to write out set theWatchedFolder to "/Volumes/Storage/Folder" or "/Users/username/Folder" in this format?

My Nov 28 10pm post showed the HFS filepath syntax to access a mounted volume, and a folder on that mounted device.


Also, is there a way for me to set multiple watched folders and target folders to look for different characters in each folder? Or do I need to make a script for each person I'm looking for? (Rocco, User B, etc.)


Well, there is a way, but it starts to get into more complicated AppleScript, which means I would have to support it, and that is not the purpose of the Apple Support Community forums. You should use single scripts until you learn how to build more complicated, single AppleScript solutions. Start your AppleScript learning here. Skip the Apple Studio tutorials as it no longer exists. When you see the clause as string, just replace it with as text, as it is the current terminology.


If I need to make different script files for each destination folder, can I have all of these files run simultaneously, as an app?

Per your previous question, one script can do all of the processing sequentially, and yes, that script can be saved as an AppleScript application. However, at this juncture, you should build a single script for each watchedFolder.

moving folders containing "name" to another folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.