Originally posted regarding using Pages to display a chart using AppleScript but was informed that this was no longer possible. Have now found that you can script Keynote to display a chart but cannot work out how to exit the slide display and loop back to request another input. As usual, any guidance would be gratefully received.
--
-- Set variables
--
property searchList : {"Want to rehome a cat", "Lost a cat", "Found a cat", "Want to get a cat neutered", "Want to give a donation", "General", "Want to adopt a cat", "Publicity"}
--
-- Input selected year and call type
--
repeat
set inputData to display dialog "Enter year and type" default answer "" buttons {"OK", "Cancel"} default button 1 with title "Call Analysis by Year"
set inputData to text returned of result as text -- get input data
set AppleScript's text item delimiters to " " -- set delimiters
--
set theYear to text item 1 of inputData -- get required year
set tx to text item 2 of inputData -- get required type
--
set filePath to quoted form of "/Users/Ours/Documents/Cats Protection/CS Notepad/Call Details.txt" -- call details
set searchItem to "/" & theYear -- extract year
set foundRecs to do shell script "grep " & searchItem & space & filePath -- only read matching records
--
set monthTotals to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} -- reset all totals
set chartData to {}
set AppleScript's text item delimiters to "£"
--
repeat with i from 1 to (number of paragraphs of foundRecs) -- search all calls
set mx to text 4 thru 5 of (text item 1 of paragraph i of foundRecs) as integer -- extract month
if (text item 5 of paragraph i of foundRecs) contains (item tx of searchList) then -- when found
set item mx of monthTotals to ((item mx of monthTotals) + 1) -- add to month
end if
end repeat
repeat with i from 1 to 12
set thisDataItem to {}
repeat 1 times
set the end of thisDataItem to item i of monthTotals
end repeat
set the end of chartData to thisDataItem
end repeat
--
tell application "Keynote"
set thisDocument to make new document with properties ¬
{height:764, width:1024, document theme:theme "Black"}
tell thisDocument
set rowNames to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
set columnNames to {"COL A"}
set rowCount to 12
set columnCount to 1
tell slide 1
set the base slide to master slide "Title - Top" of thisDocument
set the object text of the default title item to item tx of searchList & " " & theYear
add chart row names rowNames column names columnNames ¬
data chartData type stacked_vertical_bar_2d group by chart column
end tell
--set thisSlide to make new slide with properties {base slide:master slide "Title - Top"}
end tell
start thisDocument from first slide of thisDocument
end tell
end repeat
set AppleScript's text item delimiters to " "
Hi.
I don’t use Keynote myself but I’ve had some success with the following in respect of your script:
- Specifically tell Keynote to display the input dialog so that this isn’t displayed behind anything else.
- Follow the ‘start’ command with a ‘delay’ of suitable length and a ‘stop’ command. This will stop the presentation after the set interval and allow the repeat to return to the top for the next input.
- Alternatively, split the delay into shorter delays and try to set a document property between each. If this becomes possible, it means the presentation’s been stopped manually with the Escape key and there’s no need to complete the delays. It’s a bit Heath Robinson, but it seems to work!
--
-- Set variables
--
property searchList : {"Want to rehome a cat", "Lost a cat", "Found a cat", "Want to get a cat neutered", "Want to give a donation", "General", "Want to adopt a cat", "Publicity"}
--
-- Input selected year and call type
--
tell application "Keynote" to activate -- Bring Keynote to the front.
repeat
-- Tell Keynote itself to display the input dialog.
tell application "Keynote" to set inputData to display dialog "Enter year and type" default answer "" buttons {"OK", "Cancel"} default button 1 with title "Call Analysis by Year" -- with icon note
set inputData to text returned of result as text -- get input data
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " " -- set delimiters
--
set theYear to text item 1 of inputData -- get required year
set tx to text item 2 of inputData -- get required type
--
set filePath to quoted form of "/Users/Ours/Documents/Cats Protection/CS Notepad/Call Details.txt" -- call details
set searchItem to "/" & theYear -- extract year
set foundRecs to do shell script "grep " & searchItem & space & filePath -- only read matching records
--
set monthTotals to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} -- reset all totals
set chartData to {}
set AppleScript's text item delimiters to "£"
--
repeat with i from 1 to (number of paragraphs of foundRecs) -- search all calls
set mx to text 4 thru 5 of (text item 1 of paragraph i of foundRecs) as integer -- extract month
if (text item 5 of paragraph i of foundRecs) contains (item tx of searchList) then -- when found
set item mx of monthTotals to ((item mx of monthTotals) + 1) -- add to month
end if
end repeat
set AppleScript's text item delimiters to astid
repeat with i from 1 to 12
set thisDataItem to {}
repeat 1 times
set the end of thisDataItem to item i of monthTotals
end repeat
set the end of chartData to thisDataItem
-- Or simply:
-- set the end of chartData to {item i of monthTotals}
end repeat
--
tell application "Keynote"
set thisDocument to make new document with properties ¬
{height:764, width:1024, document theme:theme "Black"}
tell thisDocument
set rowNames to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
set columnNames to {"COL A"}
set rowCount to 12
set columnCount to 1
tell slide 1
set the base slide to master slide "Title - Top" of thisDocument
set the object text of the default title item to item tx of searchList & " " & theYear
add chart row names rowNames column names columnNames ¬
data chartData type stacked_vertical_bar_2d group by chart column
end tell
-- Start the one-slide presentation. The document's locked while the presentation's in progress and can't be modified.
start
-- Perform 5 seconds' worth of delays. If it becomes possible to set the document's 'auto loop' property during this time, the presentation's been stopped manually, so stop delaying early.
repeat 10 times
delay 0.5
try
set auto loop to false
exit repeat
end try
end repeat
-- Stop the presentation if it's not already stopped.
try
stop
end try
end tell
end tell
end repeat
Many thanks for the solution, I didn’t think of putting the input dialogue inside the Keynote tell block, to be honest, I didn’t realise you could do that!!
Anyway, after a bit of tweaking it now works as required, once again, thank you.