3

I have the following terminal command that I want to automate using applescript. I also would like to have a command to cd directories to the folder where I want to apply the command first. After cd to the folder that holds the .pdf files. This code looks at the first letter of the file name and based on that sorts the files into the alphabet folder corresponding to the first letter of the file.

for x in `ls -1 | sed -e 's/^\(.\).*/\1/' | sort -u`; do
mv -i ${x}?* $x
done

How would I go about doing this? I'm a newbie to this. Any insight would be greatly appreciated. I'm willing to learn Applescript, however, I'm not sure where to start to implement this.

Thanks

1 Answer 1

1

Try this:

choose folder with prompt "Select the original folder" default location (path to documents folder)

set folderPath to contents of result
set folderPath to POSIX path of folderPath

do shell script "for x in `ls -1 " & folderPath & " | sed -e 's/^\\(.\\).*/\\1/' | sort -u`; do destination=$(echo $x | tr '[:lower:]' '[:upper:]'); mkdir " & folderPath & "$destination; mv -i " & folderPath & "${x}?* " & folderPath & "$destination/; done"

It's ignoring Capitalization, i.e.: Every file starting with a and A are moved to folder A/

EDITED 1: Attempt to create the destination folder, but if it already exists, fail silently and move the files to it.

EDITED 2: Open Dropbox folder by default:

set dropboxFolder to (POSIX path of (path to home folder) & "Dropbox")
choose folder with prompt "Select the original folder" default location (POSIX file dropboxFolder as alias)

EDITRED 3: The final script:

set dropboxFolder to (POSIX path of (path to home folder) & "Dropbox")
choose folder with prompt "Select the original folder" default location (POSIX file dropboxFolder as alias)

set folderPath to contents of result
set folderPath to POSIX path of folderPath

do shell script "for x in `ls -1 " & folderPath & " | sed -e 's/^\\(.\\).*/\\1/' | sort -u`; do destination=$(echo $x | tr '[:lower:]' '[:upper:]'); mkdir " & folderPath & "$destination; mv -i " & folderPath & "${x}?* " & folderPath & "$destination/; done"
Sign up to request clarification or add additional context in comments.

9 Comments

This just changed the files name. It did not move the file without changing its name to a folder of the starting letter of the filename. Any thoughts? Thanks for your reply
I have also tried the following but cannot seem to get this to work: do script "cd ~/desktop/hazel_organization/test/test1; for x in ls -1 | sed -e 's/^(.).*/\1/' | sort -u; do mv -i ${x}?* $x done" thanks
@JPaul Just edited, it nows move to the right folder, and if the folder doesn't exist, creates it before moving the files. Hope it helps.
It is actually not ignoring capitalization of the folders. It gives an error if I start the folders with lower case letters. It still moves the files but produces an error that I can quit applescript out of. Any thoughts. Thanks so much for your help.
I've successfully applied this to a folder on my desktop, but when I try to apply this to a folder on Dropbox, it does not work. Is there something special that needs to be done to be able to apply this script to a folder on Dropbox? thanks for all your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.