Skip to content

Instantly share code, notes, and snippets.

@codingChewie
Last active March 26, 2021 16:42
Show Gist options
  • Save codingChewie/c27d2d8f09a59c5d795c5dbc0bcaa279 to your computer and use it in GitHub Desktop.
Save codingChewie/c27d2d8f09a59c5d795c5dbc0bcaa279 to your computer and use it in GitHub Desktop.
AppleScript that takes a zip archive or other type set with the fileExt variable and moves it to a directory with the same name as the file.
(*
Date: 16-12-15
Developer: codingChewie
Purpose: Takes archives and creates folder archive and moves files into it.
Version: 1.0
Name: zip-to-folder.scpt
Site: http://programmingmonkeys.com/
*)
on displayMessage(theMessage, scriptTitle)
display dialog theMessage as text with title scriptTitle
end displayMessage
on checkContent(workingFiles, workingLocation, scriptTitle, fileExt)
## loop over contents of folder
repeat with workingFile in workingFiles
## loop through contents and display each
set filename to name of workingFile
## displayMessage(filename, scriptTitle)
## get the full file's path and name
set filenamePath to POSIX path of (workingFile as text)
##displayMessage(filenamePath, scriptTitle)
## conditional for contents of folder with .epub in the filename
if filename contains fileExt then
## reverses filename so extention is first, example: piz.raboof
set revFilename to (the reverse of every character of filename) as string
## displayMessage(revFilename, scriptTitle)
## the numerical offset of the perood
set numExt to the offset of "." in revFilename
## displayMessage(numExt, scriptTitle)
## removes extension the numerical length of the period
set filenameRevNoExt to (text (numExt + 1) thru -1 of revFilename)
## displayMessage(filenameRevNoExt, scriptTitle)
## reverse the filename back to its correct name
set filenameNoExt to (the reverse of every character of filenameRevNoExt) as string
## displayMessage(filenameNoExt, scriptTitle)
## set current path and concatenate filename to the path
set fileFolderPath to POSIX path of (workingLocation) & filenameNoExt & "/"
## displayMessage(fileFolderPath, scriptTitle)
## check if the path and a directory named with the filename exists
tell application "Finder"
if not (exists folder filenameNoExt in workingLocation) then
## create a directory with the filename
set createFolder to make new folder at workingLocation with properties {name:filenameNoExt}
## move the file to the folder
move file filename in workingLocation to POSIX file fileFolderPath with replacing
## if the folder and file both exist move the file into the folder
else if (exists folder filenameNoExt in workingLocation) and (exists file filename in workingLocation) then
move file filename in workingLocation to POSIX file fileFolderPath with replacing
end if
end tell
end if
end repeat
## notify user application is complete
display notification "Application Completed" with title scriptTitle
end checkContent
on run
set scriptTitle to "Move zip to a folder" as text
set fileExt to ".zip"
try
tell application "Finder"
## request user to select folder to work on
set the workingLocation to (choose folder with prompt "Please select the directory.")
## set folder items to a variable
set workingFiles to items of workingLocation
my checkContent(workingFiles, workingLocation, scriptTitle, fileExt)
end tell
on error error_message number error_number
if error_number is equal to -128 then
display notification "Script was cancelled" with title scriptTitle
else
displayMessage(error_message, scriptTitle)
end if
end try
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment