create subfolder in subfolder and move file - applescript

i am new to this and am looking for help: i have a piece of code that is working but wanna extend it and can't make it work so far:
i have this to create a project folder in a chosen location, with two folders and two subfolders:
`set JobName to text returned of (display dialog "Please enter Project Name:" default answer "New_Project")
set loc to choose folder "Choose Parent Folder Location"
tell application "Finder"
set newfo to make new folder at loc with properties {name:JobName}
set targetFolder to make new folder at newfo with properties {name:"01_folder"}
make new folder at targetFolder with properties {name:"01_sub1"}
set targetFolder to make new folder at newfo with properties {name:"02_folder"}
make new folder at targetFolder with properties {name:"02_sub1"}
end tell`
i have two questions:
1. how can I create a subfolder in subfolder {name:"01_sub1"}?
2. how can I move a file into one of the folder that are being created?
(I know how to move to a fixed path but not one that is being chosen in the process of the same script)
i would be super thankful for any hints!
spoke

You are already doing it in your other lines so I don't understand why you have problems. You have this line:
make new folder at targetFolder with properties {name:"01_sub1"}
Just change that to capture the folder variable like this...
set subFolder1 to make new folder at targetFolder with properties {name:"01_sub1"}
Then you can put a sub folder into that with...
set anotherSubFolder to make new folder at subFolder1 with properties {name:"another subfolder"}
Note you'll also capture that folder variable so all you need to do to move a file to it is...
move someFile to anotherSubFolder
All I'm showing you is using the same logic you've already used elsewhere in the code.

Related

Automatically move files of a certain type from folder A to B whenever they are added to folder A on macOS

Using Automator, I created a "Folder Action" to move files of certain types (i.e. "Kind IS Movie") from folder "FolderA" to folder "FolderB" as soon as they are added to folder "FolderA". It works fine except that it doesn't move files from subfolders (e.g. doesn't work if the movie files are added to "FolderA/SubA/"), and apparently there's no option on Automator to make it work on the subfolders.
Does anyone have any idea about how to either change that on Automator or how could I create a script for that? Thanks!
This following AppleScript folder action code should give you a general idea of how to accomplish what you are looking for. When a file or folder is added to a folder that this folder action is attached to, it will search all folders and sub folders for files (I used QuickTime movie as the kind of file... But you can change that to whatever you want) then those files will be moved to the folder that you will define in property moveToFolder
You can save this following code in Script Editor.app directly into your /Users/Your_User_Name/Library/Workflows/Applications/Folder Actions folder, as a .scpt file. Once the script is saved into that folder, it will be available to attach to any folder as a folder action using the Folder Actions Setup.
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Kind Of File To Act Upon
property fileKind : "QuickTime movie"
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose kind is fileKind)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to
OR Here Is A Version Which Defines The Name Extensions Of The Files To Act Upon
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Name Extensions Of Files To Act Upon
property videoFileExtensions : {"m4v", "mkv", "avi", "mp4", "mov", "wmv", "mpg"}
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose name extension is in videoFileExtensions)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to

Applesricpt how to move files to /Library/Caches

I am working on a applesript code that allows users to customize the background of there login/change user screen. I am using basic applescript ui at the moment but hope to implement more advanced applescript-objC UI if this is a sucsess. At the moment i am having trouble moving the image "ch_pic" to /library/Caches can you please help me with this problem.
set ch_pic to (choose file)
--Creates variables for paths
--Creates application folder in user library for storing assets and code
tell application "Finder"
move file ch_pic to Library / Caches
end tell
--Moves user selected file to application folder
set the name of file ch_pic to "com.apple.desktop.admin.png"
If you ask i will put your name in the code of the finished product.
Pretty simple if you're just selecting, moving, and renaming a file. Just need to make sure that you're speaking the PATH style that Applescript understands.
Here is an example of the code:
-- Set ch_pic to chosen file (restricting to only recognized "image" types)
set ch_pic to (choose file of type "public.image"))
tell application "Finder"
-- Set cache_folder to Caches destination (~/Library/Caches) as Applescript path (<hard drive name>:Users:<user name>:Library:Caches)
set cache_folder to (((path to library folder from user domain as text) & "Caches"))
-- Copy the file over
copy file ch_pic to folder cache_folder
-- Grab the full Applescript path of the newly placed file (two steps for clarity. 1: set the path. 2: explicitly inform Applescript that this is a file)
set new_pic to ((cache_folder as text) & ":" & name of ch_pic)
set pos_new_pic to (file new_pic)
-- Perform the rename
set the name of pos_new_pic to "com.apple.desktop.admin.png"
end tell

Applescript: Copy all contents of a folder into every folder in a directory

I'm in a bit of a bind and an Applescript noob. I've been stumbling through a script that would allow me to copy the contents of (TemplateFolder) into all of the client folders in a directory, with the added crux that all of the existing clients' folder contents be moved into a folder within the client labeled "Old Files" or something of the sort. I'll include images with my script thus far to illustrate what I'm trying to achieve. Thank you everyone in advance for all your help.
P.S. Since I don't have the rep required, I'll have to post links to my images.
http://imgur.com/a/lL9q7
The first image is the template folder (the folder I'd like all contents copied into each client folder).
The second image is an example of an existing client folder with all the bad structure (or lack thereof).
The final image is the expected results where the template folder's contents are moved into the client folder and the original content of the client's folder are moved into a separate folder titled "Old Structure Files".
Below is the applescript I've written, with help from others, to copy the contents. However there are missing components and some elements that need to change; currently the applescript simply copies the entire folder rather than just the contents and it makes a simple copy rather than inserting, the script is not recursive for an entire directory, and there's no function to move the existing client files into a "Old Structure Files" folder. Again, any and all help is greatly appreciated :)
on run
set source_folder to choose folder with prompt "Select folder to be duplicated:" as string
my do_main_script(source_folder)
end run
on open of source_folder_list
repeat with i from 1 to number of items in the source_folder_list
set this_folder_path to item i of the source_folder_list as string
if last character of this_folder_path is ":" then
my do_main_script(this_folder_path)
end if
end repeat
end open
on do_main_script(source_folder)
tell application "Finder" to set source_folder to folder (source_folder)
tell application "Finder" to set the target_folder to (parent of source_folder)
if source_folder is not "" and target_folder is not "" then
set new_folder_name to (name of source_folder as string) & " duplicate"
set source_folder to source_folder as string
set target_folder to target_folder as string
my create_new_folder(target_folder, new_folder_name)
my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")
end if
end do_main_script
In the script bellow, the template folder is at fixed place on desktop, all the content of the customer selected folder is moved to a new folder "old structure", and after that, all content of the template folder is duplicated into the customer folder. If I well understand, this is what you're looking for :
set Templatefolder to "HD:Users:my_user:Desktop:Template"
set CustomerFolder to (choose folder with prompt "Select customer folder to be re-organised")
tell application "Finder"
set CustomerData to every item of CustomerFolder
set OldF to make new folder in CustomerFolder with properties {name:"Old Strutucture Files"}
move CustomerData to OldF
set Newstructure to every item of folder Templatefolder
duplicate Newstructure to CustomerFolder
end tell

Create folder structure on a networked drive

I'm trying to create a folder structure using applescript. This code works great locally, but when I try it on a networked drive it returns "Can't get <> "test" of application "Finder".
This is the code I have
tell application "Finder"
set campaignName to text returned of (display dialog "Please enter Campaign Name:" default answer "Campaign Name")
tell application "Finder" to set currentDirectory to target of Finder window 1
make new folder at currentDirectory with properties {name:campaignName}
make new folder at folder campaignName with properties {name:"a-creative"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"a-assets-from-client"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"b-art-director-files"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"c-for-production"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"d-developer-notes"}
make new folder at folder "b-art-director-files" of folder "a-creative" of folder campaignName with properties {name:"PSD"}
make new folder at folder "b-art-director-files" of folder "a-creative" of folder campaignName with properties {name:"storyboard"}
end tell
I'm hoping someone can help me figure out what I'm doing wrong here. I'm new to using applescript.
Thank you!
Try it this way:
tell application "Finder"
set campaignName to text returned of (display dialog "Please enter Campaign Name:" default answer "Campaign Name")
tell application "Finder" to set currentDirectory to target of Finder window 1
set projectFolderRoot to make new folder at currentDirectory with properties {name:campaignName}
set aCreativeRoot to make new folder at projectFolderRoot with properties {name:"a-creative"}
set bArtDirectorRoot to make new folder at aCreativeRoot with properties {name:"b-art-director-files"}
make new folder at aCreativeRoot with properties {name:"c-for-production"}
make new folder at aCreativeRoot with properties {name:"d-developer-notes"}
make new folder at bArtDirectorRoot with properties {name:"PSD"}
make new folder at bArtDirectorRoot with properties {name:"storyboard"}
end tell

Create Folders Applescript

I have an Applescript that I use to create job folders. The Applescript asks for the project name and the location where the folder will be stored. After inputting the job name and folder location, the script creates the main folder and four subfolders.
Now I would like the script to create numbered subfolders within one of the current subfolders, with a prompt that asks the user how many folders to create. Is that possible?
tell application "Finder"
set JobName to text returned of (display dialog "Please enter Job Name:" default answer "Job_Name")
set loc to choose folder "Choose Parent Folder Location"
set newfoldername to JobName
set newfo to make new folder at loc with properties {name:newfoldername}
make new folder at newfo with properties {name:"Job Materials"}
make new folder at newfo with properties {name:"Previews"}
make new folder at newfo with properties {name:"PSDs"}
make new folder at newfo with properties {name:"VX"}
end tell
set JobName to text returned of (display dialog "Please enter Job Name:" default answer "Job_Name")
set loc to choose folder "Choose Parent Folder Location"
tell application "Finder"
set newfo to make new folder at loc with properties {name:JobName}
make new folder at newfo with properties {name:"Job Materials"}
make new folder at newfo with properties {name:"Previews"}
set targetFolder to make new folder at newfo with properties {name:"PSDs"}
make new folder at newfo with properties {name:"VX"}
end tell
repeat
set subCount to text returned of (display dialog "How many subfolders?" default answer 3)
try
if subCount ≠ "" then
subCount as integer
exit repeat
end if
end try
end repeat
repeat with i from 1 to subCount
tell application "Finder" to make new folder at targetFolder with properties {name:"Subfolder " & i}
end repeat

Resources