I'm fairly new to learning applescript, and I'm trying to set up something for my school's announcement system in order to make it more automated. This is supposed to take the current date, check if a folder for the month exists, and if it doesn't- create one. Then it should move the announcement file into that folder.
For some reason, this worked fine until a folder was added to the desktop containing all the other folders (I don't know why, but this is quite normal for my school). Anyway, I had to edit the file path and now its not working properly. Any help would be appreciated.
property parentFolder : ((path to desktop folder) & "DESKTOP_NEWS")
on run argv
set myFile to item 1 of argv
set myMonth to month of (current date)
set currentMonthFolder to myMonth & space & "Announcements" as text
tell application "Finder"
if not (exists folder currentMonthFolder of parentFolder) is true then
make new folder at parentFolder with properties {name:currentMonthFolder}
end if
move myFile to folder currentMonthFolder of parentFolder
end tell
end run
I just tested this ( except the move part ) and it works as expected. Mainly the parent_folder was created wrongly.
property parentFolder : (((path to desktop folder) as text) & "DESKTOP_NEWS" as alias)
on run argv
set myFile to item 1 of argv
set myMonth to month of (current date)
set currentMonthFolder to (myMonth & space & "Announcements") as text
tell application "Finder"
if not (exists folder currentMonthFolder of parentFolder) then
make new folder at parentFolder with properties {name:currentMonthFolder}
end if
move myFile to folder currentMonthFolder of parentFolder
end tell
end run
Related
I have this script that I got from a MacRumors forum post, which deletes all old files in a folder:
-- Deletes all old files in Silversions folder, except the newest one.
set modDate to (15)
tell application "System Events"
set currentUser to (name of current user)
end tell
tell application "Finder"
try
delete (entire contents in folder "Silversions" of folder "From Unimportant Source" of folder "Documents" of folder "Libraries" of folder "Google Drive" of folder currentUser of folder "Users" of startup disk whose modification date is less than ((current date)) - modDate * days)
end try
end tell
Into this folder go attachments I get from particular automated emails. In theory, these will continually populate the folder, and I will always end up with the latest 15 days-worth of files. However, if the attachments are not successfully downloaded for some reason, I want to guarantee at least one file stays in the folder, which would be the most recently-gotten one.
How would I amend this script to leave the latest file in the folder?
I thought of a completely different way to accomplish this, using a little used feature introduced in OS X 10.4, Finder's sort command.
This is technically quicker.
-- Deletes all old files in Silversions folder, except the newest one.
tell application "System Events"
set currentUser to (name of current user)
end tell
tell application "Finder"
set theContainer to folder "Silversions" of folder "From Unimportant Source" of folder "Documents" of folder "Libraries" of folder "Google Drive" of folder currentUser of folder "Users" of startup disk
set sortedList to sort (get files of theContainer) by modification date
set keepName to name of (item -1 of sortedList)
delete (every file in theContainer whose name is not keepName)
end tell
display dialog "Kept file: " & keepName
Here's a flow that would do this. It basically repeats through every file in the main folder, checking its modification date, and if its newer, it stores it and deletes the previous newest file, otherwise it deletes that file.
-- Deletes all old files in Silversions folder, except the newest one.
tell application "System Events"
set currentUser to (name of current user)
end tell
tell application "Finder"
set theContainer to folder "Silversions" of folder "From Unimportant Source" of folder "Documents" of folder "Libraries" of folder "Google Drive" of folder currentUser of folder "Users" of startup disk
set newestFile to missing value
set newestDate to date "Monday, January 1, 1900 at 12:00:00 AM"
set deleteCount to 0
set fileList to files of theContainer
repeat with thisFile in fileList
set thisDate to (modification date of thisFile)
-- display dialog ("Newest: " & newestDate as string) & return & "This: " & thisDate as string
if thisDate > newestDate then
try
delete newestFile
set deleteCount to deleteCount + 1
end try
set newestFile to thisFile
set newestDate to (modification date of newestFile)
else
delete thisFile
set deleteCount to deleteCount + 1
end if
end repeat
end tell
display dialog "Deleted " & deleteCount & " files, and kept:" & return & (newestFile as string)
I am having trouble with what I think is a pretty straightforward task, but cannot seem to get my script to work correctly. I have found a lot of help through the forums with regards to the individual routines used, but it still seems to fail.
In short, what I would like to do is monitor a folder for new files being added. Once a batch of files are added (every other day or so), it will create a folder in another directory with the new folder name being the current date, move those files to the new directory, and then execute a simple bash script which uses the new directory name as an argument.
My script compiles ok, but once files are added it only creates the new folder and nothing else. I appreciate any help.
property the_sep : "-"
on adding folder items to my_folder after receiving the_files
tell application "Finder"
(* First create a new folder with name of folder = current date *)
set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
set the_name to (item 1 of my myDate())
set the_name to (the_name & the_sep & item 2 of my myDate())
set the_name to (the_name & the_sep & item 3 of my myDate())
make folder at the_path with properties {name:the_name}
set newDir to the_path & the_name
end tell
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
tell application "Finder"
try
set this_file to (item i of the_files)
move file this_file to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & newDir
end adding folder items to
on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate
The failure reason are different path styles.
AppleScript uses HFS paths (colon separated).
UNIX uses POSIX paths (slash separated).
The solution is to coerce the HFS path string to POSIX path
do shell script "qc.sh " & quoted form of POSIX path of newDir
This is a shorter version of the script using the shell also for the time stamp and for creating the directory.
on adding folder items to my_folder after receiving the_files
(* Next, move the newly added files to the source into the newly created date folder,
"path to documents" is a relative path to the documents folder of the current user *)
set baseFolder to (path to documents folder as text) & "qa:"
set timeStamp to do shell script "date +%Y-%m-%d"
set newDir to baseFolder & timeStamp
do shell script "mkdir -p " & quoted form of POSIX path of newDir
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with aFile in the_files
tell application "Finder"
try
move aFile to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & quoted form of POSIX path of newDir
end adding folder items to
Below is my code:
set sourceFolder1 to (path to library folder as text) & "Frameworks:SDL_mixer.framework"
set sourceFolder2 to (path to library folder as text) & "Frameworks:SDL_ttf.framework"
set sourceFolder3 to (path to library folder as text) & "Frameworks:SDL_image.framework"
set folderList to {sourceFolder1, sourceFolder2}
tell application "Finder"
repeat with thisFolder in folderList
if exists folder thisFolder then
delete folder thisFolder
end if
end repeat
end tell
The problem with this code is: I have to give password for every framework I want to delete (to the trash folder), Is there a way to give the password only once for all deleting work?
I cannot use shell script with rm (because rm will move files to the trash folder, but without put back option).
I notice that when the frameworks are deleted, it says delete 1 item. But when I use Finder to delete multiple files, it says delete N items. How can I implement the same thing, instead of delete 1 item each time in my code, delete N items at once, of course give password only once as well. Thanks a lot.
LJ
Try this:
tell application "Finder" to delete {sourceFolder1, sourceFolder2, sourceFolder3}
First, thank you for all responses.
It is based on the code given by user309603, a little modification has been done.
set sourceFolder1 to (path to library folder as text) & "Frameworks:SDL_mixer.framework"
set sourceFolder2 to (path to library folder as text) & "Frameworks:SDL_ttf.framework"
set sourceFolder3 to (path to library folder as text) & "Frameworks:SDLimage.framework"
set list1 to {}
tell application "Finder"
activate
if exists folder sourceFolder1 then
set list1 to {sourceFolder1}
end if
if exists folder sourceFolder2 then
set list1 to list1 & sourceFolder2
end if
if exists folder sourceFolder3 then
set list1 to list1 & sourceFolder3
end if
delete list1
end tell
Thanks again for all the help!
i've create an AppleScript very helpful to me and i wish if it is possible to automatically change the folder icon.
This script is very simple, it create one folder, then create one empty text file in the same folder.
Here is the script:
tell application "Finder"
set newfolder to make new folder with properties {name:My Folder}
make new file at newfolder with properties {name:"read_me.txt"}
end tell
Is it possible to automatically change the folder icon?
(I own my custom folder icon (.icns) in the same folder as the script, of course)
Heres a solution that uses a command line utility "seticon" found in this package: https://github.com/vasi/osxutils
It works on the assumption your script, icns file and new folder are all in the same directory.
tell application "Finder"
set parent_path to ((the container of the (path to me)) as text)
set target_folder_path to quoted form of POSIX path of (parent_path & "my folder")
set icon_path to quoted form of POSIX path of (parent_path & "icon.icns")
do shell script "/usr/local/bin/seticon -d " & icon_path & " " & target_folder_path
end tell
My Solution in 2021, using SF-Symbols
-- Help
-- folder_path : The folder whose icons will be changed (multiple selection in Finder possible)
-- icon_path : The Icon path
-- archiv_path : The Icon path for a folder named "Archiv"
-- Presets here as used by me
property folder_path : "/Users/ronny/Desktop/osxutils-master"
property icon_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Folder.icns"
property archiv_path : "/Volumes/Development/Developer/Xcode/LibPool/Icons/Archiv.icns"
-- Frameworks and Additions
use framework "Foundation"
use framework "AppKit"
use scripting additions
-- Create list (array) with selected items
-- Be carefull, every icon will be changed
tell application "Finder"
set theListe to selection as list
end tell
repeat with i in theListe
set aName to name of i
log (aName)
set destPath to POSIX path of (i as text)
if aName is equal to "Archiv" then
set sourcePath to archiv_path
else
set sourcePath to icon_path
end if
set imageData to (current application's NSImage's alloc()'s initWithContentsOfFile:sourcePath)
(current application's NSWorkspace's sharedWorkspace()'s setIcon:imageData forFile:destPath options:2)
end repeat
I have an Applescript question that is much more complex than I can construct. I have been searching for the past couple of days, and I cannot find any script like this, nor can I find enough information to piece one together with my limited knowledge.
I have multiple files with structured names. Each file has the following name structure:
ttu_collectionname_000001.pdf
ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ... etc. (Each of these files are of varying file types.)
There is also a csv metadata file associated with each of the original files.
ttu_collectionname_000001.csv
ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ... etc. (Each of these files are csv files.)
I need to create a folder based on the name of the file with sub and sub-subfolders. Each top-level folder name will be unique in the number sequence. Each sub and sub-subfolder name will be the same for each top-level folder.
The folder structure should look like this:
ttu_collectionname_000001
content
archive
display
metadata
archive
display
ttu_collectionname_000002
content
archive
display
metadata
archive
display
I then need to move the each file to a particular sub-subfolder.
The file ttu_collectionname_000001.pdf would be moved to the ttu_collectionname_000001/content/display folder.
The file ttu_collectionname_000001.csv would be moved to the ttu_collectionname_000001/metadata/display folder.
Try:
set myFolder to "Mac OS X:Users:stark:Main Folder"
tell application "Finder" to set myFiles to folder myFolder's files as alias list
repeat with aFile in myFiles
tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
do shell script "mkdir -p " & (quoted form of (POSIX path of myFolder)) & "/" & baseName & "/{\"content\",\"metadata\"}/{\"display\",\"archive\"}"
tell application "System Events"
if fileExt is "pdf" then move aFile to (myFolder & ":" & baseName & ":content:display" as text)
if fileExt is "csv" then move aFile to (myFolder & ":" & baseName & ":metadata:display" as text)
end tell
end repeat
Assuming your files are in the same folder,
this AppleScript:
set pathToFolderOfTTUFiles to (path to the desktop as text) & "TTU:"
tell application "Finder"
set theFiles to every item of folder pathToFolderOfTTUFiles whose name extension is not "csv" and kind is not "Folder"
repeat with theFile in theFiles
set lengthOfExtension to (length of (theFile's name extension as text)) + 1
set fileNameWithoutExtension to text 1 through -(lengthOfExtension + 1) of (theFile's name as text)
set theFolder to make new folder at folder pathToFolderOfTTUFiles with properties {name:fileNameWithoutExtension}
set theContentFolder to make new folder at theFolder with properties {name:"content"}
make new folder at theContentFolder with properties {name:"archive"}
set theContentDisplayFolder to make new folder at theContentFolder with properties {name:"display"}
set theMetadataFolder to make new folder at theFolder with properties {name:"metadata"}
make new folder at theMetadataFolder with properties {name:"archive"}
set theMetadataDisplayFolder to make new folder at theMetadataFolder with properties {name:"display"}
move theFile to theContentDisplayFolder
set pathToCSV to pathToFolderOfTTUFiles & fileNameWithoutExtension & ".csv"
if exists pathToCSV then move pathToCSV to theMetadataDisplayFolder
end repeat
end tell
creates this: