Remote Desktop Script

Hi,

I would like to setup a script to run periodically that would take a screenshot of random machines at a remote location outside of normal hours so that I can see if there is a problem that requires attention. Ideally, I would setup this script on the Apple Remote Desktop host machine and once or twice a day it would email me a relatively small picture of each the random clients.

Can anyone help me get started with this?

I have RD but haven’t scripted it. Maybe try an approach like this: Have RD open an observe/control window of some computer from computer list X. Take a screenshot using YOUR computer not the controlled computer. Then you can attach the file to a new message via Mail or post to FTP etc.
In RD you’ll have to get a computer by name or id or just random # (pseudo code): Tell app “RD”; set aComputer to first computer of computers of computer list “SomeMacs”; observe aComputer

I have the random part, and the number of computers to observe…

tell application "Remote Desktop"
	activate
	repeat 5 times
		observe (some computer of computer list "list")
	end repeat
end tell

Now I need help to figure out how to:

-save a screenshot of each observe window with the name of the computer being monitored in a jpg format to a folder
-Resize the images down to 640x480
-email the images

not flash but it does the trick for storing a screen shot with a name, i’m sure there are way better and more elegant ways to do this but it’s a start.

tell application "Remote Desktop"
	activate
	repeat 2 times --Set repeat count
		observe (some computer of computer list "list")
	end repeat
end tell
delay 3
tell application "System Events"
	set proc to name of first process whose frontmost is true
	tell window 1 of process proc
		set dFolder to "~/Desktop/screencapture/"
		do shell script ("mkdir -p " & dFolder)
		repeat 2 times -- Repeat as many times as above
			set NewName to get name of it
			set tTime to NewName as text
			delay 0.4
			do shell script ("screencapture " & dFolder & tTime & ".jpg") -- Capture screen.
			tell application "Remote Desktop"
				close window 1
			end tell
		end repeat
	end tell

this works for me, added the email, no error checking though, and it can be improved upon immensely
hope it helps

tell application "Remote Desktop"
	activate
	repeat 2 times --Set repeat count
		observe (some computer of computer list "list")
	end repeat
end tell
delay 3
tell application "System Events"
	set proc to name of first process whose frontmost is true
	tell window 1 of process proc
		set dFolder to "~/Desktop/screencapture/"
		do shell script ("mkdir -p " & dFolder)
		repeat 2 times -- Repeat as many times as above
			set NewName to get name of it
			set tTime to NewName as text
			delay 0.4
			do shell script ("screencapture " & dFolder & tTime & ".jpg") -- Capture screen.
			tell application "Remote Desktop"
				close window 1
			end tell
		end repeat
	end tell
	
	set _folders to alias "/Users/you/desktop/screencapture" --Path to screen capture folder
	tell application "Finder" to set theAttachment to files of _folders whose name contains ".jpg"
	
	tell application "Mail"
		activate
		set TheAddress to "YOURADDRESS@YOU.CO.NZ" --email address to whom mail is to be sent
		set newMessage to make new outgoing message with properties {visible:true, subject:"SCREEN PIC", content:"BODY TEXT" & return & return} --set visible to false so you dont want to see the mail window
		tell newMessage
			make new to recipient at end of to recipients with properties {address:TheAddress}
			tell content
				if ((count of item of theAttachment) > 0) then
					repeat with theAttachments in theAttachment
						make new attachment with properties {file name:((theAttachments) as alias)} at after the last paragraph
					end repeat
				end if
				(* --uncomment to send pics
				send newMessage -- un edit so the email will send it's self without user interaction
				tell application "Finder"
					delete (files of folder _folder whose name contains ".jpg") --so you dont keep sendingthe same files
				end tell
				*)
			end tell
		end tell
	end tell
end tell

Thanks Budgie, I will test it out as soon as i can get mail working…I was going to use my icloud account to send the emails out, but for some reason it will not let me setup an account in mail…I guess it’s just one of those days…

Budgie,

I have my mail working properly again, but I hit a couple of snags.

  1. I use three monitors and the script is not taking the screenshot of the observe windows, so i end up with a screenshot of my primary desktop monitor (applescript window). The observe windows are coming up on the secondary monitor.

  2. When it is trying to delete the contents of the folder it is giving me:
    error “Can’t make alias "Macintosh HD:Users::desktop:screencapture:" of application "System Events" into the expected type.” number -1700 from alias “Macintosh HD:Users::desktop:screencapture:”

  3. Is there a way to start a multi-observe session by selecting 5 random machines and observing the randomly selected list? That way it would send an email with only one attachment instead of 5.

Thank you so much for the help, I really appreciate it!

hi

(1) i’m not sure how to go about this

(2) Try this, it will completely delete the folder from the desktop after you have mailed the files.

tell application "Finder"
					delete (folders of desktop whose name is "screencapture" as text)
				end tell

(3) You could incorporate this into your code to create a multi observe session window.

tell application "Remote Desktop"
	activate
	set _AllList to computer list "list"
	observe _AllList
end tell

Budgie,

The folder is being deleted properly now, thanks for that tweak. I found out how to enter six random computers into an array and observe them all at the same time. I’m sure there is a better way to do the first part of this, so if you see some better way, please let me know.

tell application "Remote Desktop"
	activate
	--Picks a random list of 6 computers
	set Computer1 to (some computer of computer list "List")
	set Computer2 to (some computer of computer list "List")
	set Computer3 to (some computer of computer list "List")
	set Computer4 to (some computer of computer list "List")
	set Computer5 to (some computer of computer list "List")
	set Computer6 to (some computer of computer list "List")
	set clientlist to {Computer1, Computer2, Computer3, Computer4, Computer5, Computer6}
	
	observe clientlist --Observes the list of clients.
	
end tell

tell application "Remote Desktop"
	set bounds of front window to {50, 50, 800, 800} --Move observe window to the primary display.
	set zoomed of front window to true --corrects ARD bug that doesn't re-size the clients correctly when the window is re-sized sometimes.
end tell

delay 3
set dFolder to "~/Desktop/screencapture/"
do shell script ("mkdir -p " & dFolder)
do shell script ("screencapture ~/Desktop/screencapture/List.jpg") -- Capture screen to a file named list.jpg
tell application "Remote Desktop"
	close window 1 --Closes the observe window
end tell

tell application "Mail"
	activate
	set TheAddress to "email" --email address to whom mail is to be sent
	set newMessage to make new outgoing message with properties {subject:"Screenshots", content:"Testing" & return & return}
	set theAttachment to "Users:<user>:Desktop:screencapture:List.jpg"
	tell newMessage
		set visible to true --set visible to false so you dont want to see the mail window
		make new to recipient at end of to recipients with properties {address:TheAddress}
		tell content
			make new attachment with properties {file name:theAttachment} at after the last paragraph
			--send newMessage -- un edit so the email will send it's self without user interaction
			--tell application "Finder"
			--delete (folders of desktop whose name is "screencapture" as text)
			--end tell
		end tell
	end tell
end tell

When I run the script as it is right now, I get an error “missing value” when it is trying to attach the image and I don’t understand why that is happening. It is generating everything correctly and the resulting email looks perfect. Do i even need to worry about this if it is working? See the results of the script below.

Thanks again for kicking me in the right direction. I think this will work perfectly for my needs with a few more tweaks!

I can now trigger this script to run using a rule when an email is sent to myself which matches a specific subject and sender. Currently the script is hard coded to send the end email to a specific email.

I am now trying to figure out if it is possible for a mail.app rule to send information for applescript to read?

If I receive an email with a subject of “List A” Screenshot, can I pass the “List A” of the subject to select the six random clients from 'List A" in ARD? Then run the script and return it back to the sender who triggered the script correctly.

This way anyone who needs to check the status of the remote machines could do so just by sending an email to a specified address with the subject correctly formatted.

Thanks,
Justin

I am still trying to put all the pieces together and having some problems.

I am calling this script when an email is received by myself and the subject contains “collect screenshot”.
If I run the script in editor it will do everything up to the email part (obviously). If it runs from email, it will do the email part, and insert the file if it is already there…but it will not generate a new screenshot.

When it is working correctly, it should:

-grab 6 computers from a specific list,
-open an observe window
-move the observe window to the main display and maximize it
-take a screenshot and save it to the desktop
-reply to the sender of the message that triggered the rule and attach the new screenshot.

–The last part of this project is to read the subject line in the applescript to apply a specific list. if the subject says “collect lab screenshots” it would grab the list titled “lab”, if it says “collect office screenshots” it would grab screenshots of the list titled “office”, and so on.

Here is the script as it is now:

tell application "Remote Desktop"
	activate
	--Picks a random list of 6 computers
	set ardlist to "Lab"
	set Computer1 to (some computer of computer list ardlist)
	set Computer2 to (some computer of computer list ardlist)
	set Computer3 to (some computer of computer list ardlist)
	set Computer4 to (some computer of computer list ardlist)
	set Computer5 to (some computer of computer list ardlist)
	set Computer6 to (some computer of computer list ardlist)
	set clientlist to {Computer1, Computer2, Computer3, Computer4, Computer5, Computer6}
	
	observe clientlist --Observes the list of clients.
	
end tell

tell application "Remote Desktop"
	set bounds of front window to {50, 50, 800, 800} --Move observe window to the primary display.
	set zoomed of front window to true --corrects ARD bug that doesn't re-size the clients correctly when the window is re-sized sometimes.
end tell

delay 3
set dFolder to "~/Desktop/screencapture/"
do shell script ("mkdir -p " & dFolder)
do shell script ("screencapture ~/Desktop/screencapture/List.jpg") -- Capture screen to a file named list.jpg
tell application "Remote Desktop"
	close window 1 --Closes the observe window
end tell

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set {theSender, TheAddress, theSubject} to {extract name from sender, extract address from sender, subject}
					set theAttachment to "Users:<user>:Desktop:screencapture:List.jpg"
				end tell
				set newMessage to make new outgoing message with properties {subject:"Re: " & theSubject, visible:true, content:"Here is a screenshot of 6 random displays in the list: " & ardlist & return & return}
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theSender, address:TheAddress}
					tell content to make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
				end tell
				activate
				--send newMessage
			end tell
		end repeat
	end perform mail action with messages
end using terms from

Does anyone have any ideas on this?

Hi Magyk

Ive been mucking around with this to try and get it to work, it looks like from what I can see the part where we need to extract the info from the message headers is broken, what ive just posted works with out the extraction piece, this may be an issue when running the code from a mail rule as it appears to work when not run from a rule

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			----------------------
			-- SCREEN SHOT
			----------------------
			tell application "Remote Desktop"
				activate
				--Picks a random list of 6 computers
				set ardlist to "Lab"
				set Computer1 to (some computer of computer list ardlist)
				set Computer2 to (some computer of computer list ardlist)
				set Computer3 to (some computer of computer list ardlist)
				set Computer4 to (some computer of computer list ardlist)
				set Computer5 to (some computer of computer list ardlist)
				set Computer6 to (some computer of computer list ardlist)
				set clientlist to {Computer1, Computer2, Computer3, Computer4, Computer5, Computer6}
				observe clientlist --Observes the list of clients.
			end tell
			
			tell application "Remote Desktop"
				set bounds of front window to {50, 50, 800, 800} --Move observe window to the primary display.
				set zoomed of front window to true --corrects ARD bug that doesn't re-size the clients correctly when the window is re-sized sometimes.
			end tell
			
			delay 3
			set dFolder to "~/Desktop/screencapture/"
			do shell script ("mkdir -p " & dFolder)
			do shell script ("screencapture ~/Desktop/screencapture/List.jpg") -- Capture screen to a file named list.jpg
			tell application "Remote Desktop"
				close window 1 --Closes the observe window
			end tell
			----------------------
			-- MAIL
			----------------------
			tell application "Mail"
				
				(*--EXTRACTING INFO FROM HEADERS APPEARS TO BE BROKEN
				tell eachMessage
					set {theSender, TheAddress, theSubject} to {extract name from sender, extract address from sender, subject}
				end tell
				*)
				
				tell eachMessage
					set theSender to "TEST SENDER"
					set TheAddress to "TEST ADDRESS"
					set theSubject to "TEST SUBJECT"
				end tell
				
				set theAttachment to "Macintosh HD:Users:NAME OF USER:Desktop:screencapture:List.jpg"
				set newMessage to make new outgoing message with properties {subject:"Re: " & theSubject, visible:true, content:"Here is a screenshot of 6 random displays in the list: " & ardlist & return & return}
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theSender, address:TheAddress}
					tell content to make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
				end tell
				
				activate
				--send newMessage
			end tell
		end repeat
	end perform mail action with messages
end using terms from

I have also been trying different things, and having a hard time understanding why it will not work. I looked at the library again and found this:

Could that be used instead of creating a new message and extracting the information from the message which triggered the rule? This way the sender would be already set and then we could set the subject, content and attachment before sending the message.

Obviously i have not found out how to get it working yet…but I am still trying. There has to be something that I am missing.


set newMessage to reply msg with opening window
tell newMessage
					set content to "Here is a screenshot of 6 random displays from the list: " & "ardlist" & return & return --The "ardlist" is intentionally in "" for troubleshooting

Also, would it help to remove the Remote Desktop portion of the code and call it at the beginning of the script to generate the screenshot itself, and then let mail process without trying to call Remote Desktop itself?

Sort of like this example…http://macscripter.net/viewtopic.php?id=33743

I figured out how to reply to the email that triggered the rule properly. Now I am moving onto creating a unified rule that when triggered by the mail rule will return a screenshot of a specific list based on the content of the message.

As you can see from the code below, I am trying to learn how to use handlers.
The getscreenshot() handler seems to work fine. It’s commented out for now while I work on the next handler.

The handler that I am trying to wrap my noodle around now is my getardlist() handler. I would like to use the handler to get the result of the first word in the content of the originating message. If the content was Lab it would get the lab list, If it was office it would get the Office list, etc…

The result of this handler would then be passed to two points in the script.

  1. the content of the reply message to indicate which screenshot was taken.
  2. set the list to take a screenshot of in the getscreenshot() handler.
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with thisMessage in theMessages
				--my getardlist(ardlist) --Read the first line of the content and find out what list to view in Apple Remote Desktop
				delay 2
				--my getscreenshot() --Take a screenshot of the list called for in the content of the message
				delay 2
				set theNewMessage to reply thisMessage with opening window --Open a new reply message
				delay 2
				tell theNewMessage --Update the content to include the list that was called for, and the screenshot from Apple Remote Desktop
					set theAttachment to "Users:<user>:Desktop:screencapture:List.jpg"
					set content to "Here is a screenshot of 6 random displays from the list: " & getardlist & return & return
					delay 2
					tell content to make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
					--send newMessage
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on getardlist(ardlist) --Read the first line of the content and find out what list to view in Apple Remote Desktop
	set theContent to content of thisMessage
	if paragraph 1 of theContent is "Lab" then
		set ardlist to "Lab"
	else if paragraph 1 of theContent is "" then
		set ardlist to "All Computers"
	end if
end getardlist

on getscreenshot() --Take a screenshot of the list called for in the content of the message
	tell application "Remote Desktop"
		activate
		delay 3
		--Picks a random list of 6 computers
		set ardlist to "Lab"
		set Computer1 to (some computer of computer list ardlist)
		set Computer2 to (some computer of computer list ardlist)
		set Computer3 to (some computer of computer list ardlist)
		set Computer4 to (some computer of computer list ardlist)
		set Computer5 to (some computer of computer list ardlist)
		set Computer6 to (some computer of computer list ardlist)
		set clientlist to {Computer1, Computer2, Computer3, Computer4, Computer5, Computer6}
		
		observe clientlist --Observes the list of clients.
		
	end tell
	
	tell application "Remote Desktop"
		set bounds of front window to {50, 50, 800, 800} --Move observe window to the primary display.
		set zoomed of front window to true --corrects ARD bug that doesn't re-size the clients correctly when the window is re-sized sometimes.
	end tell
	
	delay 3
	set dFolder to "~/Desktop/screencapture/"
	do shell script ("mkdir -p " & dFolder)
	do shell script ("screencapture ~/Desktop/screencapture/List.jpg") -- Capture screen to a file named list.jpg
	tell application "Remote Desktop"
		close window 1 --Closes the observe window
	end tell
	
end getscreenshot

(*
tell application "Finder"
				delete (folders of desktop whose name is "screencapture" as text)
			end tell
*)

Lastly, when replying to a message it looks like the attachment has a problem. when it runs the code:

tell theNewMessage --Update the content to include the list that was called for, and the screenshot from Apple Remote Desktop
					set theAttachment to "Users:<user>:Desktop:screencapture:List.jpg"
					set content to "Here is a screenshot of 6 random displays from the list: " & "ardlist" & return & return
					delay 2
					tell content to make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
					--send newMessage
				end tell

It ends up in the email looking like this:

& "ardlist" &

should be

& ardlist &

I had commented that out while testing.

Since updating to 10.8, I am not able to send the message properly. Every time I try to attach the image it returns an error of missing value.

Could someone please help adjust this script?

tell application "Remote Desktop"
	activate
	--Picks a random list of 6 computers
	set ardlist to "Master List"
	set Computer1 to (some computer of computer list ardlist)
	set Computer2 to (some computer of computer list ardlist)
	set Computer3 to (some computer of computer list ardlist)
	set Computer4 to (some computer of computer list ardlist)
	set Computer5 to (some computer of computer list ardlist)
	set Computer6 to (some computer of computer list ardlist)
	set clientlist to {Computer1, Computer2, Computer3, Computer4, Computer5, Computer6}
	
	observe clientlist --Observes the list of clients.
	
end tell

tell application "Remote Desktop"
	set bounds of front window to {50, 50, 800, 800} --Move observe window to the primary display.
	set zoomed of front window to true --corrects ARD bug that doesn't re-size the clients correctly when the window is re-sized sometimes.
end tell

delay 3
set dFolder to "~/Desktop/screencapture/"
do shell script ("mkdir -p " & dFolder)
do shell script ("screencapture ~/Desktop/screencapture/List.jpg") -- Capture screen to a file named list.jpg
tell application "Remote Desktop"
	close window 1 --Closes the observe window
end tell

tell application "Mail"
	activate
	set TheAddress to "my email address" --email address to whom mail is to be sent
	set newMessage to make new outgoing message with properties {visible:true, subject:ardlist & " Screenshots", content:"Here is a screenshot of 6 random " & ardlist & " displays." & return & return}
	set theAttachment to "Users:<user name>:Desktop:screencapture:List.jpg"
	tell newMessage
		make new to recipient at end of to recipients with properties {address:TheAddress}
		make new attachment with properties {file name:theAttachment} at after last paragraph
		--send newMessage -- un edit so the email will send it's self without user interaction
	end tell
	--tell application "Finder"
	--delete (folders of desktop whose name is "screencapture" as text)
	--end tell
	
end tell

Here is the result of the script:

Can anyone help me fix this?

Hi,

the attachment is expected to be an alias and an HFS path must start with a disk name
the HFS equivalent of the POSIX path

/Users 

is

Macintosh HD:Users

Macintosh HD is the name of the startup volume

Try this


.

set theAttachment to ((path to desktop as text) & "screencapture:List.jpg") as alias
tell application "Mail"
	activate
	set TheAddress to "my email address" --email address to whom mail is to be sent
	set newMessage to make new outgoing message with properties {visible:true, subject:ardlist & " Screenshots", content:"Here is a screenshot of 6 random " & ardlist & " displays." & return & return}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:TheAddress}
		make new attachment with properties {file name:theAttachment} at after last paragraph
		--send newMessage -- un edit so the email will send it's self without user interaction
	end tell
end tell