Hi, I’m busy trying to get to grips with Aplescript Studio at the moment, and am trying to create a little app that displays man pages (amongst other things). I’ve got a window, and a button which, when clicked, should display the man page in Preview (and I got this idea and bit of code here on the Macscripter site). ANyway, here’s my code:
property keyterm:“”
on clicked theObject
set keyterm to content of text field “query” of window “QuickFind”
tell application “System Events”
set manFolderPath to (path to documents folder as Unicode text) & “Unix ‘man’ Pages (PDFs):”
set manFile to manFolderPath & keyterm & “-ManPage.pdf”
tell application “Finder”
if exists manFile then
open manFile
return
end if
end tell
do shell script ("mkdir -p " & quoted form of POSIX path of manFolderPath)
set manFilePath to quoted form of POSIX path of manFile
do shell script "/usr/bin/man -t " & quoted form of keyterm & " | /usr/bin/pstopdf -i -o " & manFilePath & "; open -a /Applications/Preview.app " & manFilePath
on error errorMsg number errorNum
display alert "Error " & errorNum message errorMsg buttons “Cancel” default button 1
end clicked
Unfortunately the script won’t compile and I can’t see what’s going wrong. Any ideas?
¢ you use on error without a try block
¢ you use tell without end tell.
¢ In System Events the right syntax is path of … folder, not path to … folder
(btw: System Events isn’t necessary to specify the path)
Furthermore I strongly recommend to avoid special characters like a single quote in file names !
I tried this successfully in “normal” AppleScript commenting out the Xcode commands
property keyterm : ""
-- on clicked theObject
set keyterm to "grep" -- content of text field "query" of window "QuickFind"
try
set manFolderPath to ((path to documents folder as Unicode text) & "Unix_man_Pages (PDFs):")
set manFile to manFolderPath & keyterm & "-ManPage.pdf"
tell application "Finder"
if exists manFile then
open manFile
return
end if
end tell
do shell script ("mkdir -p " & quoted form of POSIX path of manFolderPath)
set manFilePath to quoted form of POSIX path of manFile
do shell script "/usr/bin/man -t " & quoted form of keyterm & " | /usr/bin/pstopdf -i -o " & manFilePath & "; open -a /Applications/Preview.app " & manFilePath
on error errorMsg number errorNum
display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try
-- end clicked
Hi Stefan, thanks for geting back to me so soon. Unfortunately I can’t take credit for the original script (only my modified and non-functioning version). Here’s the original script which, if I remember rightly came from a posting on this forum (can’t remember which one tho).
on main()
set theCommand to “”
tell application “System Events”
set target_app to item 1 of (get name of processes whose frontmost is true)
end tell
tell application target_app
repeat while theCommand is ""
display dialog "View man page for this command:" default answer theCommand
set theCommand to text returned of result
end repeat
end tell
try
set manFolderPath to (path to documents folder as Unicode text) & "Unix 'man' Pages (PDFs):"
set manFile to manFolderPath & theCommand & "-ManPage.pdf"
tell application "Finder"
if exists manFile then
open manFile
return
end if
end tell
do shell script ("mkdir -p " & quoted form of POSIX path of manFolderPath)
set manFilePath to quoted form of POSIX path of manFile
do shell script "/usr/bin/man -t " & quoted form of theCommand & " | /usr/bin/pstopdf -i -o " & manFilePath & "; open -a /Applications/Preview.app " & manFilePath
on error errorMsg number errorNum
display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try
end main
main()
–tell application “Script Editor” to activate – useful if running this as a script from FastScripts; not necessary if running this as an application.
When I use the original script from script editor it works fine, as does your version. However, when I incorporate it in my AS app, as an applescript text file it won’t compile, or when I use it as an Applescript script file, clicking the button returns an error message (-1708). I’ve saved my other script steps in the app as AS text files, and they all work fine. but not this one.