Applescript creating subfolder - applescript

Good day,
I have used multiple sources to crete a workflow for creating folder structure which creates sub folders. The problem i am having is with the section that creates sub-folders under 'MyCompany" subfolder.
tell application "Finder"
set JobName to text returned of (display dialog "Please enter Job Name:" default answer "Job_Name")
set Customer to text returned of (display dialog "Customer Name:" default answer "Customer")
set loc to choose folder "Choose Parent Folder Location"
set incomingDate to (current date)
set yr to year of incomingDate as text
set mo to (month of incomingDate as number) as text
if ((length of mo) = 1) then
set mo to "0" & mo
end if
set dy to day of incomingDate as text
if ((length of dy) = 1) then
set dy to "0" & dy
end if
set newfoldername to mo & dy & yr & "-" & JobName
set customer2 to Customer & "-" & JobName
set newfo to make new folder at loc with properties {name:newfoldername}
make new folder at newfo with properties {name:"Caps"}
set doc to make new folder at newfo with properties {name:"Document"}
make new folder at doc with properties {name:"Customer"}
make new folder at doc with properties {name:"Lic"}
make new folder at doc with properties {name:"Deliverable"}
make new folder at doc with properties {name:"Mfg"}
make new folder at doc with properties {name:"Validation"}
set mdc to make new folder at newfo with properties {name:"MyCompany"}
make new folder at mdc with properties {name:"Quotes"}
make new folder at mdc with properties {name:"SoW"}
make new folder at mdc with properties {name:"Private"}
set cfg to make new folder at newfo with properties {name:"Config"}
make new folder at cfg with properties {name:"Sites"}
make new folder at cfg with properties {name:"Worksheet"}
make new folder at cfg with properties {name:"Backups"}
make new folder at cfg with properties {name:"Logs"}
set diag to make new folder at newfo with properties {name:"Diagrams"}
make new folder at diag with properties {name:"Screenshots"}
make new folder at diag with properties {name:"Visio"}
make new folder at diag with properties {name:"Logos"}
make new folder at newfo with properties {name:"Scripts"}
end tell
tell application id "com.evernote.Evernote"
create note with text "via Apple WorkFlow" title customer2 notebook "Inbox"
end tell
What I would like to have is:
mmddyy-%currenttime%-JobName
Document
MyCompany
Quotes
SoW
Private
Any help would be greatly appreciated.

Not sure if this is the cleanest way, but it worked for me:
-- Sets up the JobName, CustomerName and location by asking the user
set JobName to text returned of (display dialog "Please enter Job Name:" default answer "Job_Name")
set Customer to text returned of (display dialog "Customer Name:" default answer "Customer")
set loc to choose folder "Choose Parent Folder Location"
-- Sets up the date variables
set incomingDate to (current date)
set yr to year of incomingDate as text
set mo to (month of incomingDate as number) as text
if ((length of mo) = 1) then
set mo to "0" & mo
end if
set dy to day of incomingDate as text
if ((length of dy) = 1) then
set dy to "0" & dy
end if
-- Sets the filename to be used concatinated with date
set newfoldername to mo & dy & yr & "-" & JobName
set customer2 to Customer & "-" & JobName
tell application "Finder"
--Parent Directory Structure
set dir_1 to make new folder at loc with properties {name:newfoldername}
set dir_2 to make new folder at dir_1 with properties {name:"Caps"}
set dir_3 to make new folder at dir_1 with properties {name:"Configs"}
set dir_4 to make new folder at dir_1 with properties {name:"Diagrams"}
set dir_5 to make new folder at dir_1 with properties {name:"Documents"}
set dir_6 to make new folder at dir_1 with properties {name:"Scripts"}
-- Sub-Folder structure
set sub_3_1 to make new folder at dir_3 with properties {name:"Backups"}
set sub_3_2 to make new folder at dir_3 with properties {name:"Logs"}
set sub_3_3 to make new folder at dir_3 with properties {name:"Sites"}
set sub_3_4 to make new folder at dir_3 with properties {name:"Worksheets"}
set sub_4_1 to make new folder at dir_4 with properties {name:"Logos"}
set sub_4_2 to make new folder at dir_4 with properties {name:"Screenshots"}
set sub_4_3 to make new folder at dir_4 with properties {name:"Network Diagrams"}
set sub_5_1 to make new folder at dir_5 with properties {name:"Customer"}
set sub_5_2 to make new folder at dir_5 with properties {name:"Deliverable"}
set sub_5_3 to make new folder at dir_5 with properties {name:"License"}
set sub_5_4 to make new folder at dir_5 with properties {name:"Certificates"}
set sub_5_5 to make new folder at dir_5 with properties {name:"MyCompany"}
set sub_5_6 to make new folder at dir_5 with properties {name:"Validation"}
-- Creates sub-folders under \MyCompany
set sub2_5_1 to make new folder at sub_5_5 with properties {name:"BoMs"}
set sub2_5_2 to make new folder at sub_5_5 with properties {name:"Quotes"}
set sub2_5_3 to make new folder at sub_5_5 with properties {name:"Private"}
set sub2_5_4 to make new folder at sub_5_5 with properties {name:"SoW"}
-- Set variable to be used by Evernote
set custfldr to the loc as text
end tell
tell application "Finder"
set the clipboard to the loc as text
end tell
tell application id "com.evernote.Evernote"
create note with text custfldr title customer2 notebook "Inbox"
end tell
This gives me the desired outcome.

Related

Let user pick directory of where a created text file is placed

How do I take the user input and let that be the directory for a text file?
I tried putting quotes around the ("Inp.txt") but this doesn't read the actual user input.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Inp= InputBox("Please Enter Desired Location of Log File:")
If Inp= "" Then
Set oTF = oFSO.CreateTextFile("C:\Old Files.txt")
Else
Set oTF = oFSO.CreateTextFile(Inp.txt)
End If
I want to prompt the user for an input asking where they'd like to place their created text file; if left blank i would set it to a default location. I tried setting an inputbox prompt but when i use that as the text file location I receive an "Object Required" runtime error.
Build a complete path from the directory and a file name:
Set oTF = oFSO.CreateTextFile(oFSO.BuildPath(Inp, "output.txt"))
I would recommend letting the users browse for the folder, though.
Set app = CreateObject( "Shell.Application" )
Set d = app.BrowseForFolder(0, "Select Folder", 1, "C:\")
If d Is Nothing Then
Inp = "C:\"
Else
Inp = d.Self.Path
End If
Set oTF = oFSO.CreateTextFile(oFSO.BuildPath(Inp, "output.txt"))

Apple Script- Search Folder and Rename files

Im looking for some help with an addition to a larger apple script, ive seen lots of similar queries but none that quite fit the bill, so if anyone can help or direct me to an answer it would be a huge help,
I wanting to follow this general premise
`“Choose Name” default answer “”
set ChosenName to text returned of result
set ImagesFolder to (choose folder with prompt “Choose Images Folder:”)`
The bit im struggling with
if the ImagesFolder contains a folder named “Image Set 1” then
look through the folder “Images Set 1” and rename the contents using this logic
if file name conatins 0001_ rename file to ChosenName & “front”
if file name conatins 0002_ rename file to ChosenName & “Back”
if file name conatins 0003_ rename file to ChosenName & “Top”
if file name conatins 0004_ rename file to ChosenNamet & “Bottom”
else
if the ImagesFolder contains a folder named “Image Set 2” then
look through the folder images 2 and rename the content using this logic
if file name conatins 0001_ rename file to ChosenName & “F”
if file name conatins 0002_ rename file to ChosenName & “B”
if file name conatins 0003_ rename file to ChosenName & “T”
if file name conatins 0004_ rename file to ChosenNamet & “B”
(The unqiue characters im using to identify these files are always the last characters if this helps)
Thanks
P
This script does what you need. You need to extend it to also manage the "Image Set 2" folder and its extension name, but It will be quite easy to just duplicate what's inside the Tell "Finder" block.
Because you have multiple folder, I used a sub-routine to process your folder, each time calling new rule. For instance the 1st rule is to process "Image Set 1, search for 0001,0002,0003,0004 and replace each with Front,Back,Top, Bottom.
The rule 2 is to process "Image Set 2, search for 0001,0002,0003,0004 and replace each with F,B,T, B.
The first part build the rules. The script itself is reduced to a loop through each rule, calling the sub-routine "Process_SubFolder" with the 3 variables: sub folder name, current targets and new names.
(*
Define record Rule, made of 3 variables :
NFolderNFolder: the name of sub-folder
NSource : the list of part of file names to be processed
NDest : the list of new names. This list MUST count same number of items as NSource
All rules are added into ListRules
*)
global ChosenName, ImagesFolder -- mandatory to use in the sub-routine
set Rule to {NFolder:"Image Set 1", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"Front", "Back", "Top", "Bottom"}}
set ListRules to {Rule}
set Rule to {NFolder:"Image Set 2", NSource:{"0001", "0002", "0003", "0004"}, NDest:{"F", "B", "T", "B"}}
set ListRules to ListRules & {Rule}
set R to display dialog "Enter a name" default answer ""
set ChosenName to text returned of R
if ChosenName is "" then return -- no name selected, end of script
set ImagesFolder to choose folder with prompt "Choose Images Folder:"
repeat with aRule in ListRules
Process_SubFolder(NFolder of aRule, NSource of aRule, NDest of aRule)
end repeat
-- end of main script
on Process_SubFolder(LFolder, LSource, LDest)
tell application "Finder"
set SubFolder to (ImagesFolder as string) & LFolder
if folder SubFolder exists then
set FileList to every file of folder SubFolder -- get all files of Images Set 1
repeat with aFile in FileList -- loop through each file
set FName to name of aFile
set NewName to ""
-- Manage extension of the file
if name extension of aFile is "" then
set NewExt to ""
else
set NewExt to "." & name extension of aFile
end if
repeat with I from 1 to count of LSource --loop trhough each source of the rule
if FName contains (item I of LSource) then set NewName to ChosenName & (item I of LDest) & NewExt
end repeat
if NewName is not "" then set name of aFile to NewName -- only if name must be changed !
end repeat -- loop through files of LFolder
end if -- folder exists
end tell
end Process_SubFolder
With this structure, you can add as many rules as you want !
Of course, I assume that you will never get twice same names in sub folder ! It is not the case in Image Set 2, where you will have 2 files with new name = ChosenNameB : it will create an error !!

The variable thedData is not defined. (-2753) AppleScript

The variable thedData is not defined. (-2753)
How to fix?
set default_path to "Users/mrvisuals/Desktop"
set nuntaNume to text returned of (display dialog "Nunta Nume:" default answer "")
set nuntaData to text returned of (display dialog "Nunta Data:" default answer "")
set folderpath to (choose folder with prompt "Select Folderul Nunti" default location default_path)
set newnuntaFolder to my newFold(nuntaNume, nuntaData, folderpath)
on newFold(theNume, theData, thefolder)
set subNumeList to {"1.Steps Nunta", "2.STD", "3.TTD", "4.Album", "5.Blog", "6.Des"}
set itemCount to count of subNumeList
tell application "Finder"
set newnuntaFolder to (make new folder at thefolder with properties {name:thedData & "_" & theNume})
repeat with i from 1 to itemCount
set thisFolder to make new folder at newnuntaFolder with properties {name:"" & item i of subNumeList}
if item i of subNumeList contains "1.Steps Nunta" then
make new folder at thisFolder with properties {name:"1.Pregatiri"}
make new folder at thisFolder with properties {name:"2.First Look"}
make new folder at thisFolder with properties {name:"3.Primarie"}
make new folder at thisFolder with properties {name:"4.Biserica"}
make new folder at thisFolder with properties {name:"5.Sedinta"}
make new folder at thisFolder with properties {name:"6.Sala"}
end if
end repeat
end tell
end newFold
Your handler defines the parameter as theData:
on newFold(theNume, theData, thefolder)
but your handler later refers to it as thedData:
set newnuntaFolder to (make new folder at thefolder with properties {name:thedData & "_" & theme})
In that line, you need to change thedData to theData

create a folder in a grandparent dir vb.net

I want to create a dir (named with a varible Utilities._Name)located two levels from the exe file,
My exe file is in C:\SGEA\SGEA\bin
How can I do it so I get C:\SGEA\theNewDir without using full path, just relative paths?
If Not System.IO.Directory.Exists(Utilities._Name) Then
System.IO.Directory.CreateDirectory(Utilities._Name)
Else
MessageBox.Show("There is already a dir named: " & Utilities._Name, "SGEA", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
If you have the path to the exe file, you can use the System.IO.Path Class to navigate easily:
Dim folder = Path.GetDirectoryName(theExeFile)
Dim grandparent = Path.GetDirectoryName(Path.GetDirectoryName(folder)) ' Up two directories
Dim newFolder = Path.Combine(grandparent, "theNewDir") ' Use this to create the new folder name cleanly
Utilities._Name = newFolder

a file is copied to a specific folder and renamed to x.txt how do i open x.txt and paste "X:"

EDIT: regulus6633 has made a script that's a lot better than my outline below, it works perfectly IF you're template file isn't completely empty (I think this caused an error originally). Thanks!
This script is supposed to (1) copy a x.txt to a specific folder rename it to new_name, (2) open it, (3) paste "new_name" in all caps, and (4) insert ":" followed by return & return. The first part is working, but I'm having trouble figuring out (2), (3) and (4). The code I've written so far is pasted below.
tell application "Finder"
display dialog "new_name_dialogue" default answer " "
set new_name to (text returned of result)
set Selected_Finder_Item to (folder of the front window) as text
duplicate file "Q:x:7:n7:GTD scripting:template folder:x.txt" to "Q:X:7:SI:SIAG1"
set Path_Of_X to "Q:X:7:SI:SIAG1:" & "x.txt" as string
set name of file Path_Of_X to (new_name as text) & ".txt"
#[something that let's me open the file is needed here]
#[something that pastes "new_name" & ":" in ALL CAPS]
#[something that inserts two lineshifts]
end tell
In general since you're dealing with a txt file, you do not need to "open" the file in an application and paste in text. We can read and write to text files directly from applescript. As such we read in the text from the template file, add whatever text we want to that, and then write the new text to a new file. If you then want to open and view the new file you can do that after. I did that in the "TextEdit" section of the code.
You can see at the end of the script I have subroutines to write a text file and also to change the file name to CAPS. So try the following...
-- initial variables
set templateFile to "Q:x:7:n7:GTD scripting:template folder:x.txt"
set copyFolder to "Q:X:7:SI:SIAG1:" -- notice this path ends in ":" because it's a folder
-- get the new name
display dialog "new_name_dialogue" default answer ""
set newName to (text returned of result)
set newPath to copyFolder & newName
-- get the text of the template file
set templateText to read file templateFile
-- add the file name in CAPS, a colon, and 2 returns at the beginning of templateText
set capsName to upperCase(newName)
set newText to capsName & ":" & return & return & templateText
-- write the newText to newPath
writeTo(newPath, newText, text, false)
-- open the new file in textedit
tell application "TextEdit" to open file newPath
(*============== SUBROUTINES ==============*)
on writeTo(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as text
if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
on upperCase(theText)
set chrIDs to id of theText
set a to {}
repeat with i from 1 to (count of chrIDs)
set chrID to item i of chrIDs
if chrID ≥ 97 and chrID ≤ 122 then set chrID to (chrID - 32)
set end of a to chrID
end repeat
return string id a
end upperCase
something that lets me open the file is needed here
tell application "TextEdit" to open Path_Of_X
something that pastes new_name in ALL CAPS
There is a really good third party scripting addition (Satimage OSAX) that you can use for things just like this (this will only work if you've downloaded the scripting addition)...
set UPPER_CASE to uppercase new_name & ":"
something that inserts two lineshifts
return & return

Resources