script for job folder creation and check for duplicate

Hello-
Trying to write a script for integration with our FileMaker 7 Database to allow users to automatically create the requisite folder(s) when starting new projects, and check for an existing project folder. Our project designations are made up of a code (ie. IR001) and a name.

I started with something like this (I’m using constants for the FM values for now):



-- job info from FMPro7
set jobNumber to "IR004"
set jobName to "TestingFun"
set jobfolder_name to jobNumber & "_" & jobName

--lets make a folder!
tell application "Finder"
	with timeout of 100000 seconds
  --get the name of the current application in case an error must be reported
  set the_front_application to (every process whose frontmost is true)
  if the_front_application is {} then set the_front_application to application "Finder"
  try
  	--have the user select a destination folder and store the path to the folder in the variable: chosen_folder
  	set destination_folder to choose folder with prompt "Where should I put this project?"
  	set destination_folder to destination_folder as text
  	if folder (destination_folder & jobfolder_name) exists then
    set warningText to "A folder for this project already exists."
    display dialog warningText buttons {"OK"} default button "OK"
  	else
    make new folder in folder destination_folder with properties {name:jobfolder_name}
  	end if
  end try	
	end timeout
end tell
 

It worked, but I needed to allow a little more flexibility in checking for existing folders, as they may well not have the exact same name as what the user is putting in the DB… so I need to check all the contents of the parent folder, read the folder names, and check for the job number, since that has to be there in any instance.

I started writing my own for this instance, got confused, and ran into Jonathan Nathan’s “List Folder Contents” on this site. I modified it slightly at the end to convert its results to a string and check for my job number.


-- job info from FMPro7
set jobNumber to "IR004"
set jobName to "TestingFun"
set jobfolder_name to jobNumber & "_" & jobName
global jobFolderList
-- All the Folder Shenanigans

set the_folder to choose folder with prompt "Where should I put this project?" as string
set file_types to {} --file types, set to {} and inc_folders to true to just return folders
set with_subfolders to false --list subfolders or not (recursion)
set inc_folders to true --include folders in the list of files to return
set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
return the_files
makeFolder()

on get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
	set the_files to {}
	tell application "Finder" to set folder_list to every item of folder the_folder
	repeat with new_file in folder_list
  try
  	set temp_file_type to file type of new_file
  on error
  	set temp_file_type to "fold"
  end try
  if file_types contains temp_file_type or file_types = {} then set the_files to the_files & {new_file as string}
  if temp_file_type = "fold" then
  	if inc_folders = true and file_types ? {} then set the_files to the_files & {new_file as string}
  	if with_subfolders = true then set the_files to the_files & my get_folder_list((new_file as string), file_types, with_subfolders, inc_folders)
  end if
	end repeat
	set jobFolderList to the_files
	------- converting to string to check for duplicate project
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ", "
	set jobFolderList to jobFolderList as string
	set AppleScript's text item delimiters to oldDelimiters
	--return the_files
	return jobFolderList
end get_folder_list

This Worked, but I couldn’t get it to work WITH my other script, and it was getting really dirty. So I tried turning my folder Making into a function, pulled the string conversion and comparison into it’s own that returned a boolean, and then could make the new folder or notify the user based on that.


-- All the Folder Shenanigans
set the_folder to choose folder with prompt "Where should I put this project?" as string
set file_types to {} --file types, set to {} and inc_folders to true to just return folders
set with_subfolders to false --list subfolders or not (recursion)
set inc_folders to true --include folders in the list of files to return

-- job info from FMPro7 - (temporarily using constants for testing)
set jobNumber to "IR004"
set jobName to "TestingFun"
set jobfolder_name to jobNumber & "_" & jobName
set warningText to "A folder for this project already exists."

-- Lets run this show
set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
return the_files
set isDupe to makeStringAndCheckForDupes(the_files, jobNumber)
return isDupe
set isMade to makeFolder(the_folder, jobFolderList, jobNumber, warningText)
return isMade

-- FUNCTIONS --
on get_folder_list(the_folder, file_types, with_subfolders, inc_folders) -- countesy of Jonathan Nathan, MacScripter.net
	set the_files to {}
	tell application "Finder" to set folder_list to every item of folder the_folder
	repeat with new_file in folder_list
  try
  	set temp_file_type to file type of new_file
  on error
  	set temp_file_type to "fold"
  end try
  if file_types contains temp_file_type or file_types = {} then set the_files to the_files & {new_file as string}
  if temp_file_type = "fold" then
  	if inc_folders = true and file_types ? {} then set the_files to the_files & {new_file as string}
  	if with_subfolders = true then set the_files to the_files & my get_folder_list((new_file as string), file_types, with_subfolders, inc_folders)
  end if
	end repeat
	return the_files
end get_folder_list

on makeStringAndCheckForDupes(thelist, job)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ", "
	set thelist to thelist as string
	set AppleScript's text item delimiters to oldDelimiters
	if thelist contains job then
  return true
	else
  return false
	end if
end makeStringAndCheckForDupes

on makeFolder(the_folder, isDupe, jobNumber, warningText)
	tell application "Finder"
  with timeout of 100000 seconds
  	--get the name of the current application in case an error must be reported
  	set the_front_application to (every process whose frontmost is true)
  	if the_front_application is {} then set the_front_application to application "Finder"
  	--set visible of every process whose name is not "Finder" to false
  	try
    mount volume "afp://192.168.0.18:548/SawtoothServerProjects" as user name "InfoRetail" with password "hello"
    --have the user select a destination folder and store the path to the folder in the variable: chosen_folder
    if isDupe = true then
    	display dialog warningText buttons {"OK"} default button "OK"
    else
    	make new folder in folder the_folder with properties {name:jobfolder_name}
    end if
  	end try
  	
  end timeout
	end tell
end makeFolder

That’s where I am now, but I can’t seem to get it all to work… I’m new to applescript, and I’m not sure where it’s going wrong or if there’s a way to step through the flow with a debugger or what. Anyone see what I’m missing, or have suggestions for how to make it work another way?

Thanks-
Brendon

Got it. At the top of the file, I’m returning the_files, “is_dupe”, and “isMade” when I set those vars to call the main functions. Apparently, I don’t need to do that. :slight_smile: More importantly, returning them outside of the function appears to stop the script, which was my problem.

After I fixed that the second two functions started to run, allowing me to catch a mismatch between my parameters in the function when I wrote it and when I ran it. :slight_smile: All seems to be ducky now, and I’ve added the goodies to make a series of subfolders as well. This thing is probably pretty particular to our workflow, but just in case someone actually reads this and wants to see the finished product, here it is:


-- All the Folder Shenanigans
set the_folder to choose folder with prompt "Where should I put this project?" as string
set file_types to {} --file types, set to {} and inc_folders to true to just return folders
set with_subfolders to false --list subfolders or not (recursion)
set inc_folders to true --include folders in the list of files to return

-- job info from FMPro7 - (temporarily using constants for testing)
set jobNumber to "IR004"
set jobName to "TestingFun"
set jobfolder_name to jobNumber & "_" & jobName
set warningText to "A folder for this project already exists."

-- Lets run this show
set the_files to get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
set isDupe to makeStringAndCheckForDupes(the_files, jobNumber)
set isMade to makeFolder(the_folder, isDupe, jobfolder_name, warningText, jobNumber)

-- FUNCTIONS --
on get_folder_list(the_folder, file_types, with_subfolders, inc_folders)
	set the_files to {}
	tell application "Finder" to set folder_list to every item of folder the_folder
	repeat with new_file in folder_list
		try
			set temp_file_type to file type of new_file
		on error
			set temp_file_type to "fold"
		end try
		if file_types contains temp_file_type or file_types = {} then set the_files to the_files & {new_file as string}
		if temp_file_type = "fold" then
			if inc_folders = true and file_types ? {} then set the_files to the_files & {new_file as string}
			if with_subfolders = true then set the_files to the_files & my get_folder_list((new_file as string), file_types, with_subfolders, inc_folders)
		end if
	end repeat
	return the_files
end get_folder_list

on makeStringAndCheckForDupes(thelist, job)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ", "
	set thelist to thelist as string
	set AppleScript's text item delimiters to oldDelimiters
	if thelist contains job then
		return true
	else
		return false
	end if
end makeStringAndCheckForDupes

on makeFolder(folderParentPath, isDupe, jobfolder_name, warningText, jobNumber)
	tell application "Finder"
		with timeout of 100000 seconds
			--get the name of the current application in case an error must be reported
			set the_front_application to (every process whose frontmost is true)
			if the_front_application is {} then set the_front_application to application "Finder"
			--bring the Finder to the front
			--activate
			--hide any other active application windows
			--set visible of every process whose name is not "Finder" to false
			try
				mount volume "afp://192.168.0.18:548/SawtoothServerProjects" as user name "InfoRetail" with password "hello"
				--have the user select a destination folder and store the path to the folder in the variable: chosen_folder
				if isDupe = true then
					display dialog warningText buttons {"OK"} default button "OK"
				else
					make new folder in folder folderParentPath with properties {name:jobfolder_name}
					set folderParentPath to (folderParentPath & jobfolder_name) as string
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "CAD"}
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "Communications"}
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "FilesFromClient"}
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "Graphics"}
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "Presentation"}
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "Reference"}
					make new folder in folder folderParentPath with properties {name:jobNumber & "_" & "Releases"}
				end if
			end try
			
		end timeout
	end tell
end makeFolder