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

help pls looking for filename ending in _fin.pdf then copying to another folder

can someone help me with an applescript to look for a filename ending in _fin.pdf then copy it to another folder


thanks

KLESSER

Mac Pro

Posted on Oct 30, 2014 9:48 AM

Reply
137 replies

Oct 30, 2014 1:12 PM in response to Kevlesser

Lacking a little in details there.


Do you want to find every file on disk? in a specific folder?

Is there one file? many?

Where do you want to copy to? To a specific folder? to somewhere relative to where the source file was?


In general what you want is trivial, but the devil is in the details.


From your post, this will do what you ask, but it may not be enough depending on the missing answers to the questions.


tell application "Finder"

set srcFolder to (choose folder with prompt "Select the source folder")

set dstFolder to (choose folder with prompt "Where do you want to move the files to?")

set FilesToMove to every file of srcFolder whose name ends with "_fin.pdf"


moveFilesToMovetodstFolder

end tell


You can change srcFolder and dstFolder to be any other reference you like - in this case it prompts you to select the respective folders, but you could also hard-code a specific path here (... folder "blah" of disk "foo", ... folder "Bar" of (path to desktop), etc.)

Oct 30, 2014 3:37 PM in response to Camelot

_fin will sit in various sub folders of a specific folder on an smb mounted Nas. They should copy to only one other folder on same nas. Then maybe the original could be colour labelled once it has been copied. I would want to automate this whole process often say every 30 seconds? Colour labelling so not to repeat the copy process.

Oct 30, 2014 5:58 PM in response to Kevlesser

Kevlesser wrote:


_fin will sit in various sub folders of a specific folder on an smb mounted Nas. They should copy to only one other folder on same nas. Then maybe the original could be colour labelled once it has been copied. I would want to automate this whole process often say every 30 seconds? Colour labelling so not to repeat the copy process.


Instead of Applescript, consider using launchd to trigger the find and copy every 30 seconds. You can use the cp -n flag to skip an existing file (so no need to label the file).


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.tony.cpFiles</string>
  <key>ProgramArguments</key>
  <array>
  <string>find</string>
  <string>/NASDir</string>
  <string>-iname</string>
  <string>*_fin.pdf</string>
  <string>-exec</string>
  <string>cp</string>
  <string>-n</string>
  <string>{}</string>
  <string>/NASDir/DestFolder</string>
  <string>\;</string>
  </array>
  <key>StartInterval</key>
  <integer>30</integer>
</dict>
</plist>


Change /NASDir and /NASDir/DestFolder to the appropriate folders and then save the above as a .plist file and place in ~/Library/LaunchAgents to run when you log in, or in /Library/LaunchAgents to run when anyone logs in or in /Library/LaunchDaemons to run at system start up. (You'll need to log-out and back in the first time to take effect)

If you need to preserve file attributes, take a look at the cp options (man cp)

Oct 31, 2014 6:58 AM in response to Kevlesser

Kevlesser wrote:


tony, i am not a programmer and this is way above my head!!!


All you needed to do was copy the file I posted (as "whatever.plist") to ~/Library/LaunchAgents,

then log out and back in (or re-boot).

This is the preferred way of doing what you asked for (i.e. running a command every 30 seconds)

To learn about launchd see: Creating Launch Daemons and Agents

Oct 31, 2014 10:04 AM in response to Kevlesser

I'm not sure why (maybe someone can assist), but I cannot get the launchd to run as I posted,

however, if I put the find in a script, and have launchd run the script, it will work,


So….


1. Save this as a plain text file (i.e. NAScopy.sh), then in terminal, make it executable with the command: chmod u+x ~/Desktop/NAScopy.sh

#!/bin/bash

/usr/bin/find /NASDir -iname *_fin.pdf -exec cp -n {} /NASDir/DestFolder \;


Change /NASDir and /NASDir/DestFolder to the appropriate folders


2. Use this .plist in place of the previous one:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.tony.cpFiles</string>
  <key>ProgramArguments</key>
  <array>
  <string>/Users/Tony/Desktop/NAScopy.sh</string>
  </array>
  <key>StartInterval</key>
  <integer>30</integer>
</dict>
</plist>


Change /Users/Tony/Desktop/ to the Folder where you place the NAScopy.sh file


Sorry this got more complicated, but this worked when I tested.

Oct 31, 2014 11:46 AM in response to Tony T1

I'm not sure why (maybe someone can assist)

Add EnableGlobbing to the plist.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"

<plist version="1.0">

<dict>

<key>EnableGlobbing</key>

<true/>

I'll suggest that you add the absolute path to any commands.

<key>ProgramArguments</key>

<array>

<string>/usr/bin/find</string>

Your original Launch Agent works as prescribed.

@KLESSER

The plist name needs to be the same name as the Label with the .plist suffix.

com.tony.cpFiles.plist

You may need to logout then login for the Launch Agent to run.

help pls looking for filename ending in _fin.pdf then copying 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.