PDA

View Full Version : Moving txt file contents to clipboard




tanman627
Oct 13, 2010, 02:35 PM
I'm trying to write an applescript that utilizes VelOCRaptor (http://www.velocraptor.com/) to do the following:

A. User takes screenshot of a selected portion of the screen.
B. Screenshot is saved.
C. VelOCRaptor converts the image to a text file containing the OCR text.
D. The applescript extracts the text from this file and puts it into the clipboard.
E. Growl displays a notification telling the user how many words were copied to the clipboard.

It's part D that I'm having trouble with. I've checked and the text file outputs the text decently, but when I read the file I only get the first word or few letters of the file to copy to the clipboard. Here's the code:

# Creates a folder if it doesn't exist
on create_if_doesnt_exist(theOutputFolderPath, theNewFolderName)

tell application "Finder"
if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then
make new folder at theOutputFolderPath with properties {name:theNewFolderName}
end if
end tell

end create_if_doesnt_exist

# Path to Hard Drive library
set libraryPath to (path to library folder)

# Create the folder if it doesn't exist
create_if_doesnt_exist(libraryPath, "OCR_Output")

# Set the temporary storage folder
set tempStorage to (libraryPath & "OCR_Output" as string)

# Delete all previous contents
tell application "Finder"
-- no need to activate - this can all be done in the background
delete every item of folder tempStorage
end tell

# The path to the screenshot that is about to be taken
set picPath to ((POSIX path of tempStorage) & "/screenshot.png") as string
set outputPath to ((POSIX path of tempStorage) & "/text.txt") as string

# Capture the screenshot
do shell script "screencapture -s " & quoted form of picPath

# OCR the text
do shell script "/Applications/VelOCRaptor.app/Contents/SharedSupport/velocraptor.rb " & picPath & " " & outputPath

open for access outputPath
set fileContents to (read outputPath as text)
close access outputPath


tell application "Finder" to set the clipboard to fileContents

tell application "System Events"
set isRunning to
(count of (every process whose name is "GrowlHelperApp")) > 0
end tell

if isRunning then

tell application "GrowlHelperApp"
-- Make a list of all the notification types
-- that this script will ever send:
set the allNotificationsList to
{"OCR Text Copied to Clipboard", "OCR Text Wasn't Copied"}

-- Make a list of the notifications
-- that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to
{"OCR Text Copied to Clipboard", "OCR Text Wasn't Copied"}

-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application
"OCR to Clipboard" all notifications allNotificationsList
default notifications enabledNotificationsList
icon of application "Script Editor"
if length of fileContents = 0 then
notify with name
"OCR Text Wasn't Copied" title
"OCR Text Wasn't Copied" description
"No text was extracted from the image." application name "OCR to Clipboard"
else
-- Send a Notification...
notify with name
"OCR Text Copied to Clipboard" title
"OCR Text Copied to Clipboard" description
((count words of fileContents) as string) & " words from the text were copied to the clipboard." application name "OCR to Clipboard"
end if
end tell
end if

Anyone have any ideas as to what could be going wrong?



tanman627
Oct 13, 2010, 03:00 PM
So I figured out that I can just move the contents to the clipboard with this command: do shell script "cat " & outputPath & " | pbcopy"

The issue is that I need some way to determine when the clipboard has changed so I can then display the growl notification that the clipboard has changed.

tanman627
Oct 13, 2010, 03:23 PM
Looks like I answered my own question.

Here's the completed version for anyone who's curious:

# Creates a folder if it doesn't exist
on create_if_doesnt_exist(theOutputFolderPath, theNewFolderName)
tell application "Finder"
if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then
make new folder at theOutputFolderPath with properties {name:theNewFolderName}
end if
end tell
end create_if_doesnt_exist

# Path to Hard Drive library
set libraryPath to (path to library folder)

# Create the folder if it doesn't exist
create_if_doesnt_exist(libraryPath, "OCR_Output")

# Set the temporary storage folder
set tempStorage to (libraryPath & "OCR_Output" as string)

do shell script "rm -rf " & tempStorage & "."

# The path to the screenshot that is about to be taken
set picPath to ((POSIX path of tempStorage) & "/screenshot.png") as string
set outputPath to ((POSIX path of tempStorage) & "/text.txt") as string

# Capture the screenshot
do shell script "screencapture -s " & quoted form of picPath

# OCR the text
do shell script "/Applications/VelOCRaptor.app/Contents/SharedSupport/velocraptor.rb " & picPath & " " & outputPath

set the clipboard to "----OCR_WAITING----"
set oldClips to (the clipboard as text)

do shell script "cat " & outputPath & " | pbcopy"

set copied to false

repeat while copied is false
if ((the clipboard as text) = oldClips) = false then set copied to true
delay 1
end repeat

tell application "System Events"
set isRunning to
(count of (every process whose name is "GrowlHelperApp")) > 0
end tell

if isRunning then

tell application "GrowlHelperApp"
-- Make a list of all the notification types
-- that this script will ever send:
set the allNotificationsList to
{"OCR Text Copied to Clipboard", "OCR Text Wasn't Copied"}

-- Make a list of the notifications
-- that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to
{"OCR Text Copied to Clipboard", "OCR Text Wasn't Copied"}

-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application
"OCR to Clipboard" all notifications allNotificationsList
default notifications enabledNotificationsList
icon of application "Script Editor"
if length of (the clipboard as text) = 0 then
notify with name
"OCR Text Wasn't Copied" title
"OCR Text Wasn't Copied" description
"No text was extracted from the image." application name "OCR to Clipboard"
else
-- Send a Notification...
notify with name
"OCR Text Copied to Clipboard" title
"OCR Text Copied to Clipboard" description
((count words of (the clipboard as text)) as string) & " words from the text were copied to the clipboard." application name "OCR to Clipboard"
end if
end tell
end if