applescript timer

i am new to scripting but I’m love it so far.
any way i was foxing to wright a script do keep my desktop clean so i wont it to move things from my desk top into a temp fail when they get added but i don’t wont it to move them right away. but i can not find out how to have it count down then move the files. thanks for your help

Man,
this requires a severe investigation which files you want to collect and where to move. Furthermore, you have to organize your home folders (in which i suggest you to put your items) with a GTD-method to avoid, that the files you collect aren’t stored as garbage but for review, validate, collect, ect, ect.

you can try to collect items on the desktop, for the beginning. Like:


--define your search
set search_str to the text returned of (display dialog "Enter search term" default answer "")
tell application "Finder"
	try
		set input to make new folder at desktop with properties {name:"Collection"} --create a generic folder
	end try
	set input to folder "Collection" of desktop
	--collect files
	set the_ls to {}
	repeat with a in desktop
		set a to (a as alias)
		if (name of a as text) starts with search_str then copy a to end of the_ls
	end repeat
	--move files
	repeat with i in the_ls
		move (items of desktop whose name begins with search_str) to input
	end repeat
end tell

Furthermore, you can attach this script as folder action script to your desktop. In this case, you’ve to add

on adding folder items to this_f after receiving added_items
--your script
end adding folder items to 

Good luck!

No offence but it’s not really clear for me what you exactly want. So forgive me if my answer doesn’t full fill your question. The folder action script will be called after the file is added to the file folder or directory. This means that you are always too late when trying to delay the move action itself. The only thing you can do is rename the file with a period before the name (to make it invisible) and wait the time you want and when completed you remove the period.

to impove joys search for files with a certain name (NOTICE: this example is case sensitive)


set searchstring to "C++"
try
	set fileList to do shell script (("ls " & quoted form of POSIX path of (path to desktop folder) as string) & "*" & searchstring & "*" as string)
on error
	set fileList to {}
end try

You can delay any action the script should take by adding

delay n

as the first line (n in seconds). Be aware that the script will actually be running while it waits - don’t do this with a lot of scripts at the same time.

But maybe you should figure out why you want it to wait…
If you just want some time to look at the files you might consider not putting them on the desktop in the first place.

And check out Hazel. It was designed to solve precisely this problem - and many similar others.

He wants to delay the move action and that’s not possible because the script will be triggered after the move.

So even if the first line of code is a delay command you won’t delay the move action but only the code after the move.

i wish to move all the files added to desktop to a file /Users/ME/Desktop/temp but i dot what them to move right away
sum thing like
– get name of file
on adding folder items to desktop after receiving added_items
– delay the move
delay 60
– the move
tell application “Finder”
try
move added_items to file /Users/ME/Desktop/temp
end try
end tell

i know that the script is wrong i am only 16 and have no teacher in scripting

Well I hope I can clear this out for you.

Apple says about handler adding folder items: “A script handler that is invoked after items are added to its associated folder.”. Note that Apple writes after the items has been added. This means that you can’t delay the add action itself. Like I said before even if the first line of code is ‘delay 10’ then the move itself won’t be delayed but only the script code.

Also it doesn’t matter if you’re 16 and don’t have a teacher, we’re glad to take that role.

Any handler will run the the code it contains when “invoked”.
When the 1st line says “wait a minute” the handler will wait.

You’re saying execution of code will continue beyond the ‘delay’ line, effectively ignoring the command.
I think that is highly unlikely. Why would a folder action [handler] be any different, and ignore an arbitrary line of code?
If it won’t wait, what else is there that it would also pass over??
His (skeleton) code should work - I just don’t think it’s the best solution to his problem.

I believe you misread something. It is the script that does the moving. The handler is just a means to allow the script to be attached to a folder. All this does is let the system know that it should run the script when something is added to the folder. What happens next is up to the script: move, label, copy, or yell at the user.

I’m entirely willing to consider evidence to the contrary.

Maybe I misread something or not but TS said this:

@Alastor: I’m not saying that the first line of code will be ignored I say it won’t delay the add handler himself. You wrote this:

So i believe we have a mis-communication and thinking the same but only on a different point. I don’t say anything about the script but only about the add action itself (before the script is started by the system). When dropping a file on a folder in the Finder first the move action will be taken and then the script will run. Well the TS asks to delay the move action itself (maybe without knowing this). I replied to this as not possible because I haven’t seen a ‘should add folder items’ handler yet that asks the script for permission to move a file.

So what I’ve tried to say to TS that even if your first line of code has a delay 10, the add (user) action in the Finder isn’t delayed 10 seconds but only your script is (all the code after the delay command).

I would really like to know why you’d put a file on the desktop, only to have it moved away in a minute…

Anyway, this should work:

-- don't change 'theFolder'
-- attaching the script will take care of that
on adding folder items to theFolder after receiving added_items
	delay 60 -- wait one minute
	tell application "Finder"
		-- this folder must exist
		set targetFolder to ((desktop as text) & "temp:") as alias
		try
			move added_items to targetFolder
		end try
	end tell
end adding folder items to

Here’s a bit of explaining of how it gets the target folder.
The ‘as’ term in that line changes the class of the object; as : this is called coercion.
Take a look at this snippet of code:

tell application "Finder"
	
	-- 'desktop' returns a "reference":
	desktop --> desktop of application "Finder"
	
	-- 'alias' is a pointer to a Finder item, with its full path in HFS form:
	desktop as alias --> alias "Santanni:Users:thijs:Desktop:"
	
	-- 'text' is just that; done to extend the path string by adding "temp:"
	-- note the trailing colon, which makes it a folder
	desktop as text --> "Santanni:Users:thijs:Desktop:"
	
	-- the script does this all in one line:
	((desktop as text) & "temp:") as alias --> alias "Santanni:Users:thijs:Desktop:temp:"
end tell

In this way the script will work on any machine, in any account, as it does not need to know the name of the boot disk, or of the user.
It will also move files from any folder you attach it to, to folder “desktop:temp”.

One more thing: ‘added_items’ is a list, and I do not know if ‘move’ works on a list.
If it does not you must loop thru the list, like so:

try
	repeat with anItem in added_items
		move anItem to targetFolder
	end repeat
end try

Happy scripting!

@DJ:
Well, it seems we’re on the same path after all. No harm done, i hope.
What I was reading is that he wants to clean up his desktop, using a folder action attached to the desktop folder.
Perfectly doable.

@all: No offence against the TS (and maybe me too) but his english quality is not very good. But It seems that I have made a mistake. So my posts is not really answering his questions. I thought he want to move the files to a folder (by hand) and a script has to delay his move action. alastor933’s example cleared everything our for me and should do the job.

Again my apologies for misleading the readers.

p.s. Is there a way I can strikethrough the text of my previous posts so the readers know they shouldn’t mention my posts?

yes thanks to all your help i got alastor933 script on now and it dus just as i wanted i might tweak it when i know what im doing but for now this is prefect thanks all

iv dun sum changes and need more help


set a to (current date) as string
set b to "current date" as text
display dialog a and b buttons {"Cancel"} default button 1

will not work and dont know y


on adding folder items to theFolder after receiving added_items
	set a to (current date) as string
	set b to "current date" as text
	set c to a and b 	
	delay 18 -- wait 30 minutes
	tell application "Finder"
		-- this folder must exist
		try
			set input to make new folder at ((desktop as text) & "temp:") as alias with properties {name:c} --create a generic folder
		end try
		set targetFolder to ((desktop as text) & "temp:" & c) as alias
		try
			move added_items to targetFolder
		end try
		
	end tell
end adding folder items to

is what id like it to end up as
plezz help and can sum one tell me y it dint work
thanks

‘a and b’ is a condition. It’s like a comparison and the result should be “true/yes” or “false/no”.
‘display dialog’ can not perform that operation, it can only display text.
To string two pieces of text together you use the “concatenation operator”.
That’s the ‘ampersand’ or ‘&’.
So:

set b to "Current date: "-- it's text, no need to add 'as text'
set a to (current date) as text
display dialog b & a buttons {"OK"} default button 1

I cleaned up the code a bit.
Only use “Cancel” on dialog buttons when you want the script to stop running.

I’m not sure what you are trying to do in the script.
“Clean up the desktop” is obvious by now, but there’s more, I believe.
I have a feeling there is a better way to get to the desired outcome.
Please put into words what that desired result is - try to not use AppleScript terms.
An obvious error: ‘delay 18’ is not 30 minutes. Re-read post #4. You can put in a calculation, instead of a number.

the display dialog a & b buttons {“Cancel”} default button 1 was there to test y the script would not work and delay 18 was also just to test if the script was working i know 1800 is 30 mins
i made this script to learn more on apple script so i can make bigger scrips in the future that y i ask u to explain y things would not work in the few days of developing this script i learnt

a and b’ is a condition. It’s like a comparison and the result should be “true/yes” or “false/no”.
‘display dialog’ can not perform that operation, it can only display text.
To string two pieces of text together you use the “concatenation operator”.
That’s the ‘ampersand’ or ‘&’.

– ‘desktop’ returns a “reference”:
desktop → desktop of application “Finder”

– ‘alias’ is a pointer to a Finder item, with its full path in HFS form:
desktop as alias → alias “Santanni:Users:thijs:Desktop:”

– ‘text’ is just that; done to extend the path string by adding “temp:”
– note the trailing colon, which makes it a folder
desktop as text → “Santanni:Users:thijs:Desktop:”

– the script does this all in one line:
((desktop as text) & “temp:”) as alias → alias “Santanni:Users:thijs:Desktop:temp:”
and lots more like i said i am only 16 so it may seem simply to u but i have not had 1 class in programming and i can already understand how the simple stuff works
thanks for all your help

i found a problem but i know how to fix it but don’t know how to wright the scrip
the problem is that when i put a file on the desktop and then move it somewhere els the script moves it back to the generic folder
my way around this was a if els condition
try – move added items to generic folder
if added_items (are at the desktop) then
move added_items(desktop as text) to targetFolder
end if
the (are at the desktop) is what i don’t know how to wright

There is more.
Question (asked several times, but not answered): what is the use of the wait period?
Problem: the script will be triggered for EVERY item that appears on the desktop, so you’ll get a datestamped subfolder for EACH item
(when you drop a whole bunch they’ll all go into one folder).
Is it useful to have many folders with one item each?

The problem you found is solvable, but solving it isn’t worth it when your concept is not clear.
Here is what I think your idea or concept is:
“Every 30 minutes, collect all files on my desktop, and put them somewhere else”.
This is not the same as:
“When I drop something on my desktop I want it moved away after 30 minutes”.
Which one is it?

Every 30 minutes, collect all files on my desktop, and put them somewhere else is what id like it to do so yes my concept was not the best 1 but i have no way of knowing how to do that or even if it is possible
the delay was so when i put sum thing on the desktop i can move it away but if i dont move it away it is dun for me by the script i dont use my desktop to keep fils i have a filling metered but my desktop was always getting over ran by photos and saved work.
but i dont what to ask for sum1 els to wright the script for me id like to doit myself and just have a helping hand on the way i am going to start learning programming in grad 11 but until then people on this form will have to teach me and 1 day ill be as good as u and well be abel to do it on my owe

For your purpose a folder action is not the right workflow.
A folder action is intended to respond to user actions in a short period of time.

I suggest a script which will be triggered every 30 minutes (either as stay open script or by launchd)
and cleans up the files on the desktop whose creation or modification date is older than a specific value

so

set a to (do shell script "date '+%Y/%m/%d'") as strings
et added_items to 
repeat
	tell application "Finder"
		try --create a generic folder
			set input to make new folder at ((desktop as text) & "temp:") as alias with properties {name:"Collection " & a}
		end try
		--find the generic folder
		set targetFolder to ((desktop as text) & "temp:" & "Collection " & a) as alias
		try
			move added_items(desktop as text) to targetFolder
		end try
	end tell
	delay 60 * 30
end repeat

but what do i set added_items to. to get the added items that where added to the desktop whit out getting the files i already have on my desktop i have a dick image ( privet ) 3 folders ( temp work and games ) and 2 alias (home and apps)