This has been a goal for many an Applescripter for a long time.
Not sure anyone has hit the holly grail without using a third party scripting app with a Spotlight indexer..
I have tried a few things in the past but this thread prompted me again to have another go.
I now normally try and save all my applescripts in one places, in a Scripts folder in my Documents folder and also use Hazel to watch place like my Documents folder and desktop for scpt files and move them into the Scripts folder.
My new idea is to run a shell script that will be passed a scpt file that is saved in my Normal scripts folder as a normal scpt file and use the shell command osadecompile to decompile and read the file. Then create a text version in my new Script Text folder.
The code I am using is:
fname=`basename "$1"`
echo "`osadecompile "$1"`" > "/Users/UserName/Documents/Scripts/Script Text/$fname".txt
Hazel conveniently uses $1 for the file path of the file being passed.
And I use the command basename to get the file name.
In Hazel it looks like this.
Notice I am using a date last modified condition with 5 minutes.
This is to try and catch any edits I do.
The nice thing I found is when I find the text file with the code I am looking for, I can either copy the text, refer to it, or find the scpt file easily because I have the same file name in the text file file name.
i.e
display iTunes artwork.scpt
display iTunes artwork.scpt.txt
This idea should be able to be used with other folder watching apps or hopefully give you some inspiration ...
Updated Answer:
The code below it what I'd use to replace the original code offered as there appears to be a bug in AppleScript when using the do shell script
command in the manner originally presented that isn't present went the code, in a representative manner, is run in a bash
script in Terminal.
Note that anytimeopen for access
is used, it needs to be coded to trap any errors and try and close the file, which this attempts to do. That said it is not necessarily then only error handling I'd employ and all coding answer I present are done so as proof of concept and the onus to write code employing reasonable error handing is yours to fulfill.
As coded, this will create the target file if it doesn't exist while adding the text to add to it and if it does exist, places the text to add as the top line of the target file.
set targetFilePathname to (POSIX path of (path to desktop as string) & "My Fruit Log.txt")
-- # set theTextToWrite to "This text will be written at the top of the file." & "\n"
set theTextToWrite to "This text will be written at the top of the file." & "\n"
set theOriginalText to ""
try
set theOriginalText to (read targetFilePathname) as string
end try
-- # set theTextToWrite to theTextToWrite & "\n" & theOriginalText
set theTextToWrite to theTextToWrite & "\n" & theOriginalText
try
set referenceNumber to open for access targetFilePathname with write permission
write theTextToWrite to referenceNumber starting at 0
close access referenceNumber
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
try
close access referenceNumber
end try
return
end try
Best Answer
The problem is that the file is a plain text file. When I originally created the file in 2007, Script Editor could apparently save uncompiled
.scpt
files. Now, however, as described in Can't save AppleScript file when created by touch,.scpt
files must be compiled. Uncompiled files can be saved with the.applescript
extension.I know this is nearly a duplicate of the question linked above, but I added it because as of OS X 10.11 El Capitan Script Editor gives an error number for this problem. I couldn't find any other references to this error number so I thought it was worthwhile to get the error number in a question title.