Combine Scripts

I want to combine two scripts to make one application.

Here is the first script -


tell application "Finder"
	tell folder "Student Projects" to delete folders & files
	delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files
	empty
end tell

and the second -

set myFolder to ("Mac HD:Users:middel:Documents")

tell application "Finder"
	delete (files of folder myFolder whose name contains ".cwk")
	delete (files of folder myFolder whose name contains ".doc")
	empty trash
end tell

I’d like to change the second script so that it doesn’t have to know the name of the hard drive and user, so that anyone could use it. I’d also like to change the second script to delete files and folders like the first script, but I think I can figure that part out if someone can show me how to combine the two and then not have to use the path to the Document folder through the name of the hard drive and the user name.

Thanks =)
Connie

this is the basic concept I’m not quite sure what you are doing in the first part


set myFolder to ("" & (path to current user folder))

tell application "Finder"
	tell folder "Student Projects" to delete folders & files
	delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files
	empty
	
	delete (files of folder myFolder whose name contains ".cwk")
	delete (files of folder myFolder whose name contains ".doc")
	empty trash
end tell

Hi Connie,

path to documents folder is the documents folder of current user,
you can combine both of your scripts like this

set myFolder to path to documents folder

tell application "Finder"
	tell folder "Student Projects" to delete folders & files
	delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files
	delete (files of myFolder whose name contains ".cwk" or name contains ".doc")
	empty trash
end tell

I guess I didn’t explain very well. I am a computer tech for two schools. 2 out of 3 labs are using OS X 10.2.8. At the end of each 9 weeks, I would like to delete the contents of the Student Projects folder. Then delete all the folders and files on the desktop that are not belonging to Student Projects, Student Programs and Teacher ESheets. Lastly, I want to clean out the Documents folder in the Home folder. I’d like to make it so that if another tech decided that he/she wanted to use the program they could without having to change the name of their hard drive and the user name.

This works great -

tell application "Finder"
	tell folder "Student Projects" to delete folders & files
	delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files
	empty
end tell

This one doesn’t work for any hard drive and user name -

set myFolder to ("Mac HD:Users:middel:Documents")

tell application "Finder"
	delete (files of folder myFolder whose name contains ".cwk")
	delete (files of folder myFolder whose name contains ".doc")
	empty trash
end tell

So what I have tried to do is figure out how to get to the Documents folder without the ("Mac HD:Users:middel:Documents:) I can’t figure out how to tell it to path to the Documents folder without the name of the hard drive and users name.

The scripts posted didn’t work. I suppose that is because I need to put the part in that I don’t want to use.

I tried this -

set myFolder to (path to current user folder as Unicode text) & "Documents:"

tell application "Finder"
	tell folder "Student Projects" to delete folders & files
	delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files
	delete (files and folders of myFolder whose name is not in {"Acrobat User Data", "AppleWorks User Data", "Microsoft User Data", "Scanner Output"})
	empty trash
end tell

but it tells me that I don’t have rights because the item is owned by root.

I figure there is a way. I am just not explaining very well.

Try this:

set t to path to documents folder

PreTech

P.S. If there are student folders on several different hard drives, I don’t know if you can access the documents folder with this code however. I tried to use this code and reference another computer, but it tells me access is not allowed. Syntax could be wrong, I don’t know. :expressionless:

Well, I see that Bruce has answered while I was editing and trying stuff. So there you go!:slight_smile:

I think we all understand what you’re trying to do; It sounds like you’re just having problems with permissions.

Try taking this script.

tell application "Finder"
	try
		delete folders & files of folder "Student Projects" -- of desktop
		delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files -- of desktop
		delete (folders & files of (path to documents folder) whose name is not in {"Acrobat User Data", "AppleWorks User Data", "Microsoft User Data", "Scanner Output"})
		empty trash
	on error errMsg number errNum
		display dialog "Error " & errNum & return & return & errMsg with icon caution buttons {"OK"} default button 1
	end try
end tell

.and breaking it up to see where the problem occurs:

tell application "Finder"
	delete folders & files of folder "Student Projects" -- of desktop
end tell

tell application "Finder"
	delete (folders whose name is not in {"Student Projects", "Student Programs", "Teacher ESheets"}) & files -- of desktop
end tell

tell application "Finder"
	delete (folders & files of (path to documents folder) whose name is not in {"Acrobat User Data", "AppleWorks User Data", "Microsoft User Data", "Scanner Output"})
end tell

Bruce, I guess, that SWeeT wants to delete the stuff in all user accounts…

Those didn’t work either =(

On this one -

tell application "Finder"
   delete (folders & files of (path to documents folder) whose name is not in {"Acrobat User Data", "AppleWorks User Data", "Microsoft User Data", "Scanner Output"})
end tell

it returns this error -

Execution Error
Finder got an error: Can’t make document folder into an item.

This one -

set t to path to documents folder

gets this error -

Execution Error
Can’t get folder.

The other parts to the script work fine. I use them all the time. I wanted to add some more programs to and re image all the Macs. I have been thinking that it would be nice to be able to use one script at the end of the 9 weeks to clean everything.

There is only one user account on the computers I have.

Sorry to be a pain. I thought it would be simple.

Connie

Hi,

Using partial references is not a good idea unless you use it purposely, so I fixed up your references a little. Also, path to documents folder doesn’t work in 10.2.x.


property desktop_folders : {"Student Projects", "Student Programs", "Teacher ESheets"}
--
set docs_ref to (path to "docs")
tell application "Finder"
	tell (folder "Student Projects" of desktop)
		if exists then
			delete every item
		end if
	end tell
	set n to name of startup disk
	delete (folders of desktop whose name is not n and name is not in desktop_folders)
	delete files of desktop
	delete (files of docs_ref whose name ends with ".cwk" or name ends with ".doc")
end tell

gl,

Wow… that worked. It is a lot harder for me to understand that script. It worked perfect though. Thanks for making it =)

Connie

I can barely remember v10.2. :stuck_out_tongue: Does it use the wrong domain (local instead of user), or does it not work at all?

it doesn’t work at all indeed.

From the Scripting addition dictionary of Jaguar:

path to apple menu/application support/control panels/control strip modules/desktop/desktop pictures folder/
extensions/Folder Action scripts/fonts/help/launcher items folder/modem scripts/plugins/preferences/printer descriptions/printer drivers/printmonitor/…

– the folder or application to return
[from System domain/local domain/network domain/user domain] – where to look for the indicated folder
[as type class] – the type to return: alias or string (default is alias)
Result: alias – the path name to the folder or application specified

Thanks, StefanK. Now that I think about, I do recall seeing path to "docs" before (and it was probably posted by kel :)).

Edit: FWIW, I now recall Nigel having discussed this before. :slight_smile:

yes, but this is no contradiction.
The raw four letter code path to “docs” works also in Jaguar,
but not documents as a scripting addition enumeration

Path to “apps” works too.

Here are a few more ;):

http://developer.apple.com/documentation/Carbon/Reference/Folder_Manager/index.html

In OS 10.2 “ and right back to at least OS 8.0 and probably earlier “ the keyword version of this was path to At Ease documents folder, which still works today, although it’s been superseded. It was quite confusing because it was named long ago after an Apple technology of the time and later scripters didn’t know what it meant. Also, there was some technical reason why it wasn’t possible to display it in the Standard Additions dictionary. The equivalent for the Applications folder was path to At Ease applications folder. Many people preferred the string equivalents path to “docs” and path to “apps” because they involved much less typing and were probably less confusing. The default path to domain for the Documents folder is the user domain, but somewhere between OS 10.2 and 10.4, the default domain for the Applications folder has changed from user to local domain, which makes more sense.

Here are the shortcuts that work for me (I suspect those with 501 in them are user-specific - your mileage may vary. Note also that Æ’ is option-f):

Enumeration Shortcuts
macs:-- Your_HD:System:
desk:-- Your_HD:Users:YourShortName:Desktop:
trsh:-- Your_HD:Users:YourShortName:.Trash:
prnt:-- Your_HD:System:Library:Printers:PrintMonitor Documents:
font:-- Your_HD:System:Library:Fonts:
pref:-- Your_HD:Users:YourShortName:Library:Preferences:
sprf:-- Your_HD:System:Library:PreferencePanes:
temp:-- Your_HD:private:var:tmp:folders.501:TemporaryItems:
apps:-- Your_HD:Applications:
docs:-- Your_HD:Users:YourShortName:Documents:
root:-- Your_HD:System:
asup:-- Your_HD:Library:Application Support:
flnt:-- Your_HD:private:var:tmp:folders.501:Cleanup At Startup:
Æ’mod:-- Your_HD:System:Library:Modem Scripts:
Æ’hlp:-- Your_HD:Library:Documentation:Help:
Æ’lib:-- Your_HD:System:Library:CFMSupport:
utiÆ’:-- Your_HD:Applications:Utilities:
aexÆ’:-- Your_HD:Applications:Extras:
cmnu:-- Your_HD:Library:Contextual Menu Items:
favs:-- Your_HD:Users:YourShortName:Library:Favorites:
prof:-- Your_HD:System:Library:ColorSync:Profiles:
dtpÆ’:-- Your_HD:Library:Desktop Pictures:
issf:-- Your_HD:Users:YourShortName:Library:Internet Search Sites:
fnds:-- Your_HD:System:Library:Find:
ilgf:-- Your_HD:Library:Receipts:
scrÆ’:-- Your_HD:Users:YourShortName:Library:Scripts:
fasf:-- Your_HD:Users:YourShortName:Library:Scripts:Folder Action Scripts:
rapp:-- Your_HD:Users:YourShortName:Library:Recent Applications:
rdoc:-- Your_HD:Users:YourShortName:Library:Recent Documents:
rsvr:-- Your_HD:Users:YourShortName:Library:Recent Servers:
kchn:-- Your_HD:Users:YourShortName:Library:Keychains:
qtex:-- Your_HD:System:Library:QuickTime:

Hi everybody,

Thanks Nigel for the explaination. I couldn’t remember which special folder references go with At Ease, so began using the four letter codes.

FYI, there is a file you can search for named “Folders.h” which contains four letter codes the system uses. You want the one in the System folder. I keep a copy on the side, when i forget what the code is. Adam’s list looks very extensive, but there might be more. Didn’t Rob post a list somewhere on this site?

SWeet, the script is not that different. All I did was complete the references, check for existance of the project folder, and combine your filter references into one statement. Also, ‘items of desktop/ returns the startup disk, which you can’t delete, so added that to the filter reference. When you just use the name of the folder. AppleScript looks for the folder in the current directory. It used to be that the current directory was the folder that the current application was located in. In osx they chnged it, so that the current folder is the startup disk. So, if you try to delete a folder by name only and there is a folder with that name at the root level, then the script will delete that folder first. If there isn’t a folder with that name in the startup disk, then I think it jumps to the desktop. Anyway, you should use complete references. In filter reference forms (… where/whose property1 is value1 and propert2 is value2 …), you can make multiple comparisons with the ‘and’ or "or’ operator. Here, you don’t want to use:

name contains “.cwk” or name contains “.doc”

because there might be a file named something like “somefile.cwk.txt”. If your intention was to delete only AppleWorks and Word files, then you would delete a wrong file. It’s probably not that imprtant though in your case.

gl,

I haven’t included all those in Folders.h. I’ll pursue that further. All I do is grab the text following space & “=” & space & “'” and before “'” & space. There are 135 of them, but many of them error in a try block, referring to pre-OS X entities like extensions folder.