AppleScript ⇢ Working with Files and Folders

Hey Folks,

This topic is about using AppleScript to work with Files and Folders, and I'll periodically add more goodies to it.


Let's start out by finding items in a directory corresponding to a certain regular expression.

In this case items whose name contains “screen shot”.

The macro produces a text-list of the paths of all found items.

The find command can be recursive, although the default is NOT recursive.

There are only 3 lines of code germane to the average user ⇢ see “User Settings”.

-Chris


Find Files using a Regular Expression.kmmacros (4.1 KB)

2 Likes

Doing a similar job with plain vanilla AppleScript and string-matching instead of using a regular expression is quite easy.

As you can see I’m moving the Files found with this script to a different destination.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/03/24 12:33
# dMod: 2018/03/24 12:39 
# Appl: System Events
# Task: Move files whose name contains a string to a destination folder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Move, @Files, @Whose, @Name, @Contains, @String, @Destination, @Folder
----------------------------------------------------------------

set sourceFolder to path to desktop folder
set destinationFolder to alias ((path to downloads folder as text) & "Test ⇢ Destination Folder:")

tell application "System Events"
   set fileList to (files of sourceFolder whose name contains "screen shot")
   move fileList to destinationFolder
end tell

----------------------------------------------------------------

-Chris

2 Likes

Deleting items older than a given number of days in a given directory.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/03/24 13:00
# dMod: 2018/03/24 13:06
# Appl: Finder, System Events
# Task: Trash items from a directory that are older than a given number of days.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events, @Trash, @Delete, @Items, @Directory, @Older, @Days
----------------------------------------------------------------

set sourceFolder to alias ((path to home folder as text) & "test_directory:Test_Delete_by_Time:")
set obsoleteDate to (current date)
set time of obsoleteDate to 0 --> changing the time to Midnight
set obsoleteDate to obsoleteDate - (31 * days)

tell application "System Events"
   set itemList to items of sourceFolder whose creation date is less than obsoleteDate
   repeat with i in itemList
      set contents of i to (path of i) as alias
   end repeat
end tell

tell application "Finder" to delete itemList

----------------------------------------------------------------

NOTE – The path to the source directory is an alias – NOT a POSIX Path.

See Putting Relative-Aliases on the Clipboard for how to create an alias with a Keyboard Maestro macro.

-Chris

4 Likes

Hello @ccstone and thanks a lot for your script.

I have a question (I'm not well versed with applescript to say the least).

What do you mean by " NOTE – The path to the source directory is an alias – NOT a POSIX Path." and the first line of your script:

set sourceFolder to alias ((path to home folder as text) & "test_directory:Test_Delete_by_Time:")

Let's say my folder has a path of "/Users/benmac16/Dropbox/300-APP-SYNC/SSH-Config-Backup" (I guess that's what is called a POSIX path, whereas I've always named that a simple "path").

How do I "convert" that path to use your script ? Or how do I modify your script to work with classic POSIX path ? (if that's even possible)

Is it just using this as first line ?

set sourceFolder to "/Users/benmac16/Dropbox/300-APP-SYNC/SSH-Config-Backup"

I don't understand what the "alias ((path to home folder as text) & "test_directory:Test_Delete_by_Time:")" means in your script and since it's a delete stuff script, I'd rather ask before whipping my hard drive's content :laughing:

Thanks a lot,

Ben