2.1 Make files and folders

Under the standard suite of the finder dictionary you can find "make". You can create a new folder or a new file with the command (verb) make.

2.1.1 ex: make a folder

tell application "Finder"
    make new folder
end tell

2.1.2 ex: make a file

tell application "Finder"
    make new file
end tell

2.1.3 ex: make a folder at a specific location

tell application "Finder"
    set p to path to desktop -- Or whatever path you want
    make new folder at p with properties {name:"New Folder"}
end tell

2.1.4 example: create a folder, and create a file within it

tell application "Finder"
    set p to path to desktop -- Or whatever path you want
    make new folder at p with properties {name:"Applescript_Test_Folder"}
 
    set e to (path to desktop folder as string) & "Applescript_Test_Folder" -- Path to folder we just created
    make new file at e with properties {name:"Applescript_Test_File"}
end tell

2.2 Deleting items

You can delete items using the delete command

2.2.1 ex: delete folder items and file items

try
    tell application "Finder"
        delete (every item of folder (path to desktop folder) whose name begins with "Screen Shot")
        delete (every item of folder (path to downloads folder) whose name ends with ".jpg")
    end tell
on error
    display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try

Be careful! This deletes items and their contents!!!

You can modify it yourself to point to a different folder or to target different files. If you remove the qualifier "whose name begins with" and leave the line as "delete (every item of folder (path to desktop folder))", then you can delete all the contents of a folder in one fell swoop.

2.2.2 ex: handler to delete files and folders with undo

This handler works with both files and folders - targeting a folder with allowUndo set to false therefore permanently deletes that folder's entire subtree.

on MyDeleteProc(theFile, allowUndo)
  if allowUndo then
    tell application "Finder" to delete theFile as POSIX file
  else
    tell application "System Events" to delete alias theFile
  end if
end MyDeleteProc

2.2.3 ex: delete using shell script "rm" or "rm -rf"

Just using rm only works with files - if you wanted to extend it to folders, too, use rm -rf instead - the same caveat re permanently deleting entire subtrees applies.

on MyDeleteProc(theFile, allowUndo)
  if allowUndo then
    tell application "Finder" to delete theFile as POSIX file
  else
    do shell script "rm " & quoted form of theFile
  end if
end MyDeleteProc

Note the use of quoted form of, which safely passes the file path to the shell, encoding characters such as spaces properly.

2.3 Setting the path to files and folders

There are three ways to write out a path:

Type Syntax
Explicit at folder "Documents" of folder "smerth" of folder "Users" of folder "Macintosh HD"
POSIX at "/Macintosh HD/Users/user/Documents/"
Colon Notation at "Macintosh HD:User:Documents"

When using POSIX notation for the path, note the preceding and trailing slashes...

2.3.1 ex: specify a path using explicit notation

tell application "Finder"
  make new folder at folder "Desktop" of folder "user" of folder "Users" of folder "Macintosh HD" with properties {name:"My_Super_Folder"}
end tell

However this path uses the user name. To make the path generic (not specific to a user's name) you can write: at (path to documents folder)

This works for: documents, downloads, desktop, applications

2.3.2 ex: Specify path to documents, downloads, desktop, and applications

-- set path to documents folder
tell application "Finder"
  make new folder at (path to documents folder) with properties {name:"My_Super_Folder"}
end tell
 
-- set path to download folder
tell application "Finder"
  make new folder at (path to downloads folder) with properties {name:"My_Super_Folder"}
end tell
 
-- set path to desktop folder
tell application "Finder"
  make new folder at (path to desktop folder) with properties {name:"My_Super_Folder"}
end tell
 
-- set path to applications folder
tell application "Finder"
  make new folder at (path to applications folder) with properties {name:"My_Super_Folder"}
end tell

To make it easier when setting a path to a file or a folder, set a variable for the path and append that using &...

2.3.3 ex: appending a path to documents, downloads, desktop, and applications folder location

-- example 1
tell application "Finder"
    set myFolderPath to (path to documents folder as string) & "Test"
    make new folder at myFolderPath with properties {name:"Test_2"}
end tell
 
-- example 2
tell application "Atom"
    set filePath to (path to desktop folder as string) & "test.txt"
    open filePath
end tell

… The folder you are targeting must exist...

2.4 Moving files and folders

Now that you can handle paths you can try moving files and folders.

tell application "Finder"
    set myFolderPath to (path to documents folder as string) & "Test"
    move folder myFolderPath to (path to desktop folder)
end tell

2.5 Read properties of a file

ToDo...

2.6 Add Properties to files and folders

When creating files and folders you can add properties to them. Look up properties you want to add using the dictionary. Curly braces are used to hold a list of key value pairs for the properties you want to add.

tell application "Finder"
    make new folder with properties {name:"newFolder"}
end tell

Looking up the file Class in the dictionary we find a list of properties we can interact with.

file n [inh. item] : A file

properties

file type (type) : the OSType identifying the type of data contained in the item

creator type (type) : the OSType identifying the application that created the item

stationery (boolean) : Is the file a stationery pad?

product version (text, r/o) : the version of the product (visible at the top of the “Get Info” window)

version (text, r/o) : the version of the file (visible at the bottom of the “Get Info” window)

The file Class inherits from the item Class so we can also interact with the item properties.

The dictionary lists the item properties.

item n : An item

elements

contained by application, containers, disks, folders, desktop-objects, trash-objects.

properties

name (text) : the name of the item

displayed name (text, r/o) : the user-visible name of the item

name extension (text) : the name extension of the item (such as “txt”)

extension hidden (boolean) : Is the item's extension hidden from the user?

index (integer, r/o) : the index in the front-to-back ordering within its container

container (specifier, r/o) : the container of the item

disk (specifier, r/o) : the disk on which the item is stored

position (point) : the position of the item within its parent window (can only be set for an item in a window viewed as icons or buttons)

desktop position (point) : the position of the item on the desktop

bounds (rectangle) : the bounding rectangle of the item (can only be set for an item in a window viewed as icons or buttons)

label index (integer) : the label of the item

locked (boolean) : Is the file locked?

kind (text, r/o) : the kind of the item

description (text, r/o) : a description of the item

comment (text) : the comment of the item, displayed in the “Get Info” window

size (double integer, r/o) : the logical size of the item

physical size (double integer, r/o) : the actual space used by the item on disk

creation date (date, r/o) : the date on which the item was created

modification date (date) : the date on which the item was last modified

icon (icon family) : the icon bitmap of the item

URL (text, r/o) : the URL of the item

owner (text) : the user that owns the container

group (text) : the user or group that has special access to the container

owner privileges (read only/‌read write/‌write only/‌none)

group privileges (read only/‌read write/‌write only/‌none)

everyones privileges (read only/‌read write/‌write only/‌none)

information window (specifier, r/o) : the information window for the item

properties (record) : every property of an item

class (type, r/o) : the class of the item

2.6.1 example: add name property

tell application "Finder"
    make new folder at "Macintosh HD" with properties {name:"My_new_folder"}
end tell

2.6.2 example: the name property with extension

tell application "Finder"
    set filename to "NewFile"
    make new file at (path to desktop folder) with properties {name:filename & ".md"}
end tell

For information about setting file type and extensions see Uniform Type Identifier Concepts

2.7 Check if a file exists.

Under the standard suite in the dictionary you will find the command "exists"

2.7.1 ex: fileExists

tell application "Finder"
    set fileExists to exists of (file "my_empty_file" of (path to desktop folder))
end tell

Here you are setting the result of the exists command executed on a file at a speficied path to the value of the varible fileExists. That result of the exists command is the boolean. So the value of fileExists variable will be true or false.

There are applications that overwrite the standard exists command like "System Events" and "Finder". So to use the exists command on a file to check if the file exists you should wrap your code in an tell application "System Events" or "Finder" block.

2.7.2 ex: check exists with system events

set theFile to "/Users/wrong user name/Desktop"
 
--using system events 
tell application "System Events" to set fileExists to exists disk item (my POSIX file theFile as string)
--using finder
tell application "Finder" to set fileExists to exists my POSIX file theFile
--using alias coercion with try catch
try
    POSIX file theFile as alias
    set fileExists to true
on error
    set fileExists to false
end try
--using a do shell script
set fileExists to (do shell script "[ -e " & quoted form of theFile & " ] && echo true || echo false") as boolean
--do the actual existence check yourself
--it's a bit cumbersome but gives you an idea how an file check actually works
set AppleScript's text item delimiters to "/"
set pathComponents to text items 2 thru -1 of theFile
set AppleScript's text item delimiters to ""
set currentPath to "/"
set fileExists to true
repeat with component in pathComponents
    if component is not in every paragraph of (do shell script "ls " & quoted form of currentPath) then
        set fileExists to false
        exit repeat
    end if
    set currentPath to currentPath & component & "/"
end repeat
return fileExists