Creating an Applescript that will export user input to html

I was wondering if anyone has ever tried to make an applescript that will take information entered into a dialog box and then export the input to html, or even a PDF document?

The purpose of this script is to help me locate the vandals that are tweaking some of my settings on school computers.

I was thinking for my computer lab to have the student enter their name into a dialog box, have the computer do a quick snapshot and then output the results to an HTML or PDF.

The other consideration/problem is that the applescript will continue to add to the html document as another student uses the computer.

So here is my thought:

  1. have the user enter their name into a dialog box

  2. have the computer take a snapshot with the isight camera

  3. have the applescript generate an html document with the results and a time/date stamp of when the user entered this information

  4. repeat and continue to add to the information as another user does the same thing.

Hi,

this is all possible.
First, get iSightCapture and save it somewhere.
This script creates the HTML file, takes the pictures and inserts the data into the HTML file. All files will be saved in imageFolder
Change the property lines to your appropriate values. Consider the POSIX path of iSightCapture and the HFS path with trailing colon of imageFolder.
The script is quite simple just with basic error handling


property title : "titleOfHTMLSite"
property pictureWidth : 320
property pictureHeight : 240
property iSightCapture : "/path/to/isightcapture"
property imageFolder : "disk:path:to:imageFolder:"
property fileName : "test.html"

set theName to text returned of (display dialog "Enter your name" default answer "")

try
	set ff to open for access file (imageFolder & fileName) with write permission
	if ((get eof ff) = 0) then
		set sourceText to createHTMLtext()
	else
		set sourceText to read ff
	end if
	set {imagePath, timeStamp} to takePicture(imageFolder)
	set sourceText to addPicture(sourceText, imagePath, theName, timeStamp)
	set eof ff to 0
	write sourceText to ff
	close access ff
on error
	try
		close access file (imageFolder & fileName)
	end try
end try

on takePicture(f)
	set timeStamp to do shell script "/bin/date +%Y%m%d-%H%M%S"
	set filePath to POSIX path of f & timeStamp & ".jpg"
	do shell script iSightCapture & " -w " & pictureWidth & " -h " & pictureHeight & " -t jpg " & quoted form of filePath
	return {filePath, timeStamp}
end takePicture

on addPicture(htmlText, imagePath, imageDescription, ti)
	set {TID, text item delimiters} to {text item delimiters, "</div>"}
	set {s1, s2} to text items of htmlText
	set text item delimiters to TID
	set width to quote & (pictureWidth as text) & quote
	set height to quote & (pictureHeight as text) & quote
	return s1 & "<p><img src=\"" & imagePath & "\" alt=\"\" height=" & height & " width=" & width & " border=\"0\">  " & imageDescription & " (" & ti & ")</p>
</div>" & s2
end addPicture

on createEndTag(t)
	return "</" & t & ">" & linefeed
end createEndTag

on createHTMLtext()
	set htmlText to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
<title>" & title & createEndTag("title") & createEndTag("meta") & createEndTag("head")
	return htmlText & "<body bgcolor=\"#ffffff\">" & linefeed & "<div align=\"left\">" & linefeed & createEndTag("div") & createEndTag("body") & createEndTag("html")
end createHTMLtext

Thank you, this is exactly what I was looking for.

I have one last question for you:

When I run this as an applescript application there are no problems, but when I paste the applescript into automator I get this error message "Syntax Error Expected “end” but found “property”. And automator highlights the first property reference in the applescript.

Any ideas, how I can work around that? I was wanting to include the script along with other basic settings I have for the computer lab (there are a series of display adjustments, volume, ect… this script).

Regards,
paulmattallen

replace

property <key> : <value>

with

set <key> to  <value>

Thank you Stefan

set title to "Computer User Log"
set pictureWidth to 320
set pictureHeight to 240
set iSightCapture to "/Applications/isightcapture" --path to iSightCapture
set imageFolder to "tester:Users:Shared:imageFolder:" --path to imageFolder/
set fileName to "computer_user_log.html"
set theName to "test"

try
	set ff to open for access file (imageFolder & fileName) with write permission
	if ((get eof ff) = 0) then
		set sourceText to createHTMLtext()
	else
		set sourceText to read ff
	end if
	set {imagePath, timeStamp} to takePicture(imageFolder)
	set sourceText to addPicture(sourceText, imagePath, theName, timeStamp)
	set eof ff to 0
	write sourceText to ff
	close access ff
on error
	try
		close access file (imageFolder & fileName)
	end try
end try

on takePicture
	set timeStamp to do shell script "/bin/date +%Y-%m-%d-%H%M"
	set filePath to POSIX path of f & timeStamp & ".jpg"
	do shell script iSightCapture & " -w " & pictureWidth & " -h " & pictureHeight & " -t jpg " & quoted form of filePath
	return {filePath, timeStamp}
end takePicture

on addPicture(htmlText, imagePath, imageDescription, ti)
	set {TID, text item delimiters} to {text item delimiters, "</div>"}
	set {s1, s2} to text items of htmlText
	set text item delimiters to TID
	set width to quote & (pictureWidth as text) & quote
	set height to quote & (pictureHeight as text) & quote
	return s1 & "<p><img src=\"" & imagePath & "\" alt=\"\" height=" & height & " width=" & width & " border=\"0\">  " & imageDescription & " (" & ti & ")</p>
</div>" & s2
end addPicture

on createEndTag(t)
	return "</" & t & ">" & linefeed
end createEndTag

on createHTMLtext()
	set htmlText to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
<title>" & title & createEndTag("title") & createEndTag("meta") & createEndTag("head")
	return htmlText & "<body bgcolor=\"#ffffff\">" & linefeed & "<div align=\"left\">" & linefeed & createEndTag("div") & createEndTag("body") & createEndTag("html")
end createHTMLtext

Ok so I made the changes you suggested on AppleScript Editor, but when I try to port it over to Automator I get the following error…

The error I get now says "Expected “end” but found “on” …this is listed on takepicture

Any help would be greatly appreciated

Regards,
paulmattallen

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

in Automator you need to wrap (only !) the implicit run handler of your script with

on run {input, parameters}
	
	(* Your script goes here *)
	
	return input
end run

Ok, so I was unable to include the application in automator. So I thought I would try a different method. What I did was I saved the applescript as an application entitled “photo_log”.

Then I was trying in automator to use the applescript

run application "photo_log"

But I get the following error “error “photo_log got an error: Connection is invalid.” number -609”.

I am uncertain what the hangup is here, what is the connection?

The applescript runs fine if I save it as an standalone application or run as an applescript, but once I link to it with automator I encounter errors.

Any help would be greatly appreciated. I am certain I mess something up, but I am not sure what exactly.

Regards,
paulmattallen

Or I get the same error if I use the following script

 run application "photo_log"
delay 1
run application "login_items"

Where photo_log is the HTML photo, login items is the application that changes system settings for the students computers.

I get the error “photo_log got an error: Connection is invalid.”

Thanks again,
paulmattallen

run application is not the proper syntax, it must be first launched or activated

tell application "photo_log"
	launch
	run
end tell

This works wonderfully thanks again my friend.

I have one question about how to edit the generated HTML document.

I would like to add in the HTML the title in h1 lettering, and if possible the date range. This will help with filing/bookkeeping…

something like

Computer User Log

From the dates [earliest date] to [last date]

here is the applescript so far



property title : "Computer #12 User Log"
property pictureWidth : 320
property pictureHeight : 240
property iSightCapture : "/Applications/isightcapture"
property imageFolder : "tester:Users:Shared:imageFolder:"
property fileName : "test_computer_login.html"

set theName to ""

try
	set ff to open for access file (imageFolder & fileName) with write permission
	if ((get eof ff) = 0) then
		set sourceText to createHTMLtext()
	else
		set sourceText to read ff
	end if
	set {imagePath, timeStamp} to takePicture(imageFolder)
	set sourceText to addPicture(sourceText, imagePath, theName, timeStamp)
	set eof ff to 0
	write sourceText to ff
	close access ff
on error
	try
		close access file (imageFolder & fileName)
		display dialog "Error... Image was not taken." with icon stop with title "Error" giving up after 5
		quit
		quit
		
	end try
end try

on takePicture(f)
	set timeStamp to do shell script "/bin/date +%m-%d-%Y-%H%M"
	set filePath to POSIX path of f & timeStamp & ".jpg"
	do shell script iSightCapture & " -w " & pictureWidth & " -h " & pictureHeight & " -t jpg " & quoted form of filePath
	return {filePath, timeStamp}
end takePicture

on addPicture(htmlText, imagePath, imageDescription, ti)
	set {TID, text item delimiters} to {text item delimiters, "</div>"}
	set {s1, s2} to text items of htmlText
	set text item delimiters to TID
	set width to quote & (pictureWidth as text) & quote
	set height to quote & (pictureHeight as text) & quote
	return s1 & "<p><img src=\"" & imagePath & "\" alt=\"\" height=" & height & " width=" & width & " border=\"0\">  " & imageDescription & " (" & ti & ")</p>
</div>" & s2
end addPicture

on createEndTag(t)
	return "</" & t & ">" & linefeed
end createEndTag

on createHTMLtext()
	set htmlText to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
<title>" & title & createEndTag("title") & createEndTag("meta") & createEndTag("head")
	return htmlText & "<body bgcolor=\"#ffffff\">" & linefeed & "<div align=\"left\">" & linefeed & createEndTag("div") & createEndTag("body") & createEndTag("html")
end createHTMLtext
quit
quit
quit

I am just not sure how to add that extra information.

Regards,
paulmattallen


property title : "Computer #12 User Log"
property pictureWidth : 320
property pictureHeight : 240
property firstTimeStamp : ""
property iSightCapture : "/Applications/isightcapture"
property imageFolder : "tester:Users:Shared:imageFolder:"
property fileName : "test_computer_login.html"

set theName to ""
set timeStamp to do shell script "/bin/date +%m-%d-%Y-%H%M"
try
	set ff to open for access file (imageFolder & fileName) with write permission
	if ((get eof ff) = 0) then
		set sourceText to createHTMLtext()
		set firstTimeStamp to timeStamp
	else
		set sourceText to read ff
	end if
	set imagePath to takePicture(imageFolder, timeStamp)
	set sourceText to addPicture(sourceText, imagePath, theName, timeStamp)
	set sourceText to insertDate(sourceText, timeStamp)
	set eof ff to 0
	write sourceText to ff
	close access ff
on error
	try
		close access file (imageFolder & fileName)
		display dialog "Error... Image was not taken." with icon stop with title "Error" giving up after 5
		return
	end try
end try

on takePicture(f, stamp)
	set filePath to POSIX path of f & stamp & ".jpg"
	do shell script iSightCapture & " -w " & pictureWidth & " -h " & pictureHeight & " -t jpg " & quoted form of filePath
	return filePath
end takePicture

on addPicture(htmlText, imagePath, imageDescription, ti)
	set {TID, text item delimiters} to {text item delimiters, "</div>"}
	set {s1, s2, s3} to text items of htmlText
	set text item delimiters to TID
	set width to quote & (pictureWidth as text) & quote
	set height to quote & (pictureHeight as text) & quote
	return s1 & "</div>" & s2 & "<p><img src=\"" & imagePath & "\" alt=\"\" height=" & height & " width=" & width & " border=\"0\">  " & imageDescription & " (" & ti & ")</p>
</div>" & s3
end addPicture

on insertDate(htmlText, ti)
	set {TID, text item delimiters} to {text item delimiters, "<h3>"}
	set {s1, s2} to text items of htmlText
	set text item delimiters to "</h3>"
	set s3 to text item 2 of s2
	set text item delimiters to TID
	return s1 & "<h3>From the dates " & firstTimeStamp & " to " & ti & "</h3>" & s3
end insertDate

on createEndTag(t)
	return "</" & t & ">" & linefeed
end createEndTag

on createHTMLtext()
	set htmlText to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
<title>" & title & createEndTag("title") & createEndTag("meta") & createEndTag("head") & "<body bgcolor=\"#ffffff\">" & linefeed
	set htmlText to htmlText & "<div align=\"center\">" & linefeed & "<h1>" & title & "</h1>" & linefeed & "<h3></h3>" & linefeed & createEndTag("div") & linefeed
	return htmlText & "<div align=\"left\">" & linefeed & createEndTag("div") & createEndTag("body") & createEndTag("html")
end createHTMLtext

Note: your multiple quit statements are not necessary