Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 13, 2010, 03:35 PM   #1
tanman627
macrumors newbie
 
Join Date: Oct 2010
Moving txt file contents to clipboard

I'm trying to write an applescript that utilizes VelOCRaptor 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:

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 is offline   0 Reply With Quote
Old Oct 13, 2010, 04:00 PM   #2
tanman627
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Alternative remedy, still problems

So I figured out that I can just move the contents to the clipboard with this command:
Code:
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 is offline   0 Reply With Quote
Old Oct 13, 2010, 04:23 PM   #3
tanman627
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Got it

Looks like I answered my own question.

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

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)

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
tanman627 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Applescript - formatting a txt file using write dam316 Mac Applications and Mac App Store 0 May 4, 2014 06:26 PM
Resolved: Trouble reading a .txt file in Xcode with C++ rayonix Mac Programming 2 Jan 4, 2014 05:03 PM
Xcode 4.6. Can't open .txt file thrill4rishabh Mac Programming 6 May 28, 2013 02:22 PM
File rename based on file contents--possible? Brutalbrutus Mac Basics and Help 2 Jul 17, 2012 06:57 AM
Shell: paste + retrieve contents of clipboard. fhill2 Mac Programming 11 Jun 21, 2012 04:39 PM

Forum Jump

All times are GMT -5. The time now is 07:47 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC