New to Apple Script looking for your friendly help!

What I’m trying to do is write a AppleScript that I can check folders either to remove all files from them or a certain file.

In this test script I wrote it works fine but I was unable to get the variable currentUser to replace JackDamore in the path to delete all files from the folder testQ. I tried putting it in there and i received a error. I also tried quotes around it that didn’t work either.

I will be going into multiple locations to delete files a examples would be ~/Library/… and /Library/… even /usr/lib/… as some of the locations.

Being new to AppleScripting I would appreciate a good example to work from and learn these technics.

tell application “System Events”

set currentUser to (name of current user)

end tell

set folderOne to “testQ”
tell application “Finder”
delete (files of folder (“Macintosh HD:Users:JackDamore:Downloads:” & folderOne))
end tell

say currentUser

Any of your fine help would be appreciated I am not a programmer but a new iMac user that wants to learn more.
BigJack :slight_smile:


Model: iMac with i& and 8gigs of ram
AppleScript: 2.3.3
Browser: Safari Version 7.1 (9537.85.10.17.1)
Operating System: Mac OS X (10.8)

If I understand correctly the question, this draft may help :

The given code didn’t do the entire job so I removed it.

Yvan KOENIG (VALLAURIS, France) mercredi 24 septembre 2014 18:47:54

I was unable to get the variable currentUser to replace JackDamore in the path to delete all files from the folder testQ.

Was the main help I was looking for to grab the (name of current user) put into a variable and then put it into my delete patch.

BigJack

Hi, this deleted all files except Keynote files why would not delete those file types?

Thanks BigJack

I guess that it’s because for System Events Keynote documents which are packages are not files but folders.

Try with :

The given code failed so I removed it.

Yvan KOENIG (VALLAURIS, France) mercredi 24 septembre 2014 19:26:18

This gives a error message and doesn’t run. Any other ideas?

I don’t understand why the late one fails and would be interested to read an explanation.
This edited one works.

tell application "System Events"
	
	set currentUser to (name of current user)
	
end tell

set userHome to path to home folder as text
-->> "<startup Volume>:Users:<current User>:"
set folderOne to "testQ"
tell application "System Events"
	set theItems to every disk item of folder (userHome & "Downloads:" & folderOne) # EDITED
	repeat with anItem in theItems
		delete disk item (path of anItem)
	end repeat
end tell

say currentUser

An alternate answer is :

tell application "System Events"
	
	set currentUser to (name of current user)
	
end tell

set userHome to path to home folder as text
-->> "<startup Volume>:Users:<current User>:"
set folderOne to "testQ"
tell application "Finder"
	delete every file of folder (userHome & "Downloads:" & folderOne) # EDITED
end tell

say currentUser

It doesn’t use a loop but the files are not really deleted, they are moved to the trash.

Yvan KOENIG (VALLAURIS, France) mercredi 24 septembre 2014 21:43:15

Ok a issue I have found may be you awesome experts can help me!

This code deletes all the files in a folder: “ALL”

set folderOne to “testQ”

tell application “Finder”
delete (files of folder (“Macintosh HD:Users:JackDamore:Downloads:” & folderOne))
end tell


This codes deletes all the files except Keynotes and RTF files?

So what is that happing? I’m baffled by this!

tell application “System Events”

set currentUser to (name of current user)

end tell

set userHome to path to home folder as text

set folderOne to “testQ”

tell application “System Events”
delete (files of folder (userHome & “Downloads:” & folderOne))

end tell

say currentUser

say userHome

I already wrote that your Keynote documents are not files but package (“file package” in System Events vocabulary)
During my tests, the original code removed every flatfiles including rtf ones.

It failed to remove Keynote, Numbers, Pages packages as well as rtfd documents which are packages too.

Yvan KOENIG (VALLAURIS, France) mercredi 24 septembre 2014 22:00:38

May you delete the thread entitled : Ok a issue I have found may be you awesome experts can help me with. by BigJack which duplicate this one ?

This is the final code that works! It deletes all files!

tell application “System Events”

set currentUser to (name of current user)

end tell

set userHome to path to home folder as text
–>> “:Users::”

set folderOne to “testQ”

tell application “Finder”
delete (files of folder (userHome & “Downloads:” & folderOne))
–”>> delete every file of folder “:Users::Downloads:testQ”
end tell

say currentUser

say userHome

Hi BigJack,

One thing, you can get the path to the current users download folder like this:

tell application "System Events"
	set downloadsFolderPath to (path to downloads folder as string)
end tell

Edited: also, with Finder references, you can use partial references like this:

tell application "System Events"
	set downloadsFolder to (path to downloads folder)
end tell
tell application "Finder"
	delete (every item of folder "TestQ" of downloadsFolder)
end tell

i.e. it uses a Finder reference mixed with alias reference.

gl,
kel

path to is a part of Standard Additions, there is no need to tell System Events


set downloadsFolder to path to downloads folder

On the other hand, the relative folder specifiers are integrated in System Events, so you could write


tell application "System Events"
	set downloadsFolder to downloads folder
end tell


Hi Stefan,

You are right. Thanks for pointing that out. I was in a rush to play with my new toy. :slight_smile:

Have a good day!
kel