Hi
As a newbie I am passing through a hard time. I post my problem on applescript forum but I got the reply that i could do this with smile or script debugger.
Now i found this part of forum where people are talking about smile and script debugger.
Can any body tell me is it possible or not and if yes please let me know.
Here is my problem
I have a script
property fileName : "xxxxxxx .pdf"
set myPath to (path to me as string)
set AppleScript's text item delimiters to ":"
set the parentFolder to ¬
((text items 1 thru -2 of myPath) & "") as string
set AppleScript's text item delimiters to ""
try
set targetFile to alias (the parentFolder & fileName)
on error
return quit
end try
tell application "Finder"
open file targetFile
end tell
Now I want to replace the first line of code with
property fileName : "yyyyyy .pdf"
thats not difficult but I want to do this without opening the script
I want a script that on run find the word ‘xxxxxx.pdf’
and replace it with ’ yyyyyy.pdf’. I want to do this because I have thousands of script to modify.
somebody suggested me that it is only possble with smile or script debugger.
I don’t know such features, but you can use this script instead:
--> though to use as an app, via drag&drop
--> so, save as app and drop some test scripts
--> should also work in applets/droplets
on open bunchOfScripts
repeat with i in bunchOfScripts
if file type of (info for i) = "osas" then --> this is a script, go ahead!
doStuff(i)
end if
end repeat
display dialog "Done!"
end open
to doStuff(i)
set oldScript to (load script i) as text --> the script as plain text
set searchFor to "property fileName : "xxxxxxx .pdf""
set replaceWith to "property fileName : "yyyyyyyy.pdf""
--> replace stuff
set newScript to replacetext(searchFor, replaceWith, oldScript)
--> recompile script
set newScript to run script "return foo" & return & ¬
"script foo" & return & ¬
newScript & return & ¬
"end script"
--> replace old script with new one
store script newScript in i replacing yes
end doStuff
to replacetext(searchFor, replaceWith, thisText)
tell (a reference to AppleScript's text item delimiters)
set contents to searchFor
set thisText to thisText's text items
set contents to replaceWith
set thisText to "" & thisText
set contents to {""}
end tell
return thisText
end replacetext
And this version supports applets/droplets, and data-fork scripts:
on open bunchOfScripts
repeat with i in bunchOfScripts
if file type of (info for i) = "osas" then --> this is a script, go ahead!
doStuff(i)
end if
end repeat
display dialog "Done!"
end open
to doStuff(i)
set oldScript to (load script i) as text --> the script as plain text
set searchFor to "property fileName : "xxxxxxx .pdf""
set replaceWith to "property fileName : "yyyyyyyy.pdf""
--> replace stuff
set newScript to replacetext(searchFor, replaceWith, oldScript)
try
read i --> if there are contents, this is a data fork file
--> if (most probably an app instead of a datafork script) error and go to store to fork
if (result's length = 9116) and ("/usr/lib/dyld" is in contents of result) then error
--> empty data fork and write new script
set fileRef to (open for access i with write permission)
set eof of fileRef to 0
write newScript to fileRef
close access fileRef
--> compile it
set i to quoted form of POSIX path of i
do shell script "osacompile -d -o " & i & space & i
on error --> end of file = store in resource fork
--> recompile script
set newScript to run script "return foo" & return & ¬
"script foo" & return & ¬
newScript & return & ¬
"end script"
store script newScript in i replacing yes
end try
end doStuff
to replacetext(searchFor, replaceWith, thisText)
tell (a reference to AppleScript's text item delimiters)
set contents to searchFor
set thisText to thisText's text items
set contents to replaceWith
set thisText to "" & thisText
set contents to {""}
end tell
return thisText
end replacetext
Sorry I was put on some other project
Now i am back to solve this problem.
Thanks JJ
i checked the script that you suggested but it doesn’t work.
the script is not changing the content of my script
Do you have some more suggestions
I’m very interested in this script, so I’ll make it work… :!:
Maybe you forgot adjusting these lines?
set searchFor to "property fileName : "xxxxxxx .pdf""
set replaceWith to "property fileName : "yyyyyyyy.pdf""
Or you really want substitute:
property fileName : "xxxxxxx .pdf"
With:
property fileName : "yyyyyyyy.pdf"
If so, please, note the space between the “xxxxxxx” & “.pdf”
I’ve seen also that the “open” handler restricts the applet/droplet features. So, if you wish processing any app-script, you should delete the “if file type of…” and “end if”, so we have a single “doStuff(i)”…
Let me know! (and play before with some test scripts before processing all!!!)
thanks jj for interest my project
but I think I am doing some thing wrong
here is what I am doing according to your suggestions
I adjust,simplify and remove the space from .pdf
set searchFor to "property fileName : "xxx.pdf""
set replaceWith to "property fileName : "yyy.pdf""
and idon’t exactly understand here what you said
But I tried to remove
but make no difference
Here is what over all Iam doing
i am copy and paste your script and make changes as you said and save as application and i drop this file
property fileName : "xxx.pdf"
set myPath to (path to me as string)
set AppleScript's text item delimiters to ":"
set the parentFolder to ¬
((text items 1 thru -2 of myPath) & "") as string
set AppleScript's text item delimiters to ""
try
set targetFile to alias (the parentFolder & fileName)
on error
return quit
end try
tell application "Finder"
open file targetFile
end tell
on the droplet created by your script and it give me result “Done”
but it doesn’t change my code from “xxx.pdf” to “yyy.pdf”
please give some suggestion
if I am not clear please let me know
thanks once again
I created a simple droplet and a folder containing 4 kinds of process-able scripts. All these scripts include the code “beep 5”.
If you drop them onto “search-replace applescript-code”, you will be asked for “search string” and “replace string”. If you enter both “beep 5” as search and “beep 3” as replacement, all should work ok…
I found some problems when copy-pasting the code from here to my script editor, so I think it is better in the www.
Here is a link with the stuff (60 Kb)… http://homepage.mac.com/julifos/soft/beta/test.sit
:!: Please, work with a copy of your scripts!!!
And here is the source-code:
property searchFor : missing value
property replaceWith : missing value
on open theScript
set filesToProcess to {}
repeat with i in theScript
set fileInfo to (info for (alias (i as string)))
if file type of fileInfo = "osas" or file type of fileInfo = "APPL" then
try --> avoid invalid files and run-only scripts
set isValid to (load script file (i as string))
set end of filesToProcess to i as alias
end try
end if
end repeat
if filesToProcess = {} then
display dialog "No valid files found!" & return & "So, I will quit..." & return & return & "NOTE: Valid files are editable applescripts (compiled or apps)..." with icon note buttons {"OK"} default button 1
return
end if
--> get search/replace values for ALL scripts
repeat
if searchFor = "" then set searchFor to text returned of (display dialog "Please, enter a string to search for..." & return & return default answer "property x : 215" with icon note)
if searchFor ? "" then set replaceWith to text returned of (display dialog "Please, enter a string to replace "" & searchFor & ""..." & return & return default answer "property x : 375" with icon note)
if searchFor ? "" then exit repeat
end repeat
display dialog "Replace "" & searchFor & "" with "" & replaceWith & "" in every script?..." with icon note
--> process every valid script
repeat with i in filesToProcess
set oldScript to (load script i) as text --> the script as plain text
--> replace stuff
set newScript to my replacetext(oldScript)
try
read i --> if there are contents, this is a data fork file
--> if (most probably an app instead of a datafork script) error and go to store to fork (FEED FACE)
if (result starts with "?Ì?Œ") and ("/usr/lib/dyld" is in contents of result) then error
--> empty data fork and write new script
set fileRef to (open for access i with write permission)
set oldFileContents to (read fileRef)
set eof of fileRef to 0
write newScript to fileRef
close access fileRef
--> compile it
set i to quoted form of POSIX path of i
try
do shell script "osacompile -d -o " & i & space & i
on error msg --> ?
display dialog "Error osacompiling:" & return & msg buttons {"OK"} default button 1 with icon caution
set fileRef to (open for access i with write permission)
set eof of fileRef to 0
write oldFileContents to fileRef
close access fileRef
end try
on error msg --> end of file or applet/droplet = store in resource fork
--> recompile script
set newScript to run script "return foo" & return & ¬
"script foo" & return & ¬
newScript & return & ¬
"end script"
store script newScript in i replacing yes
end try
end repeat
beep 2
end open
to replacetext(thisText)
tell (a reference to AppleScript's text item delimiters)
set contents to searchFor
set thisText to thisText's text items
set contents to replaceWith
set thisText to "" & thisText
set contents to {""}
end tell
return thisText
end replacetext
Hmmm… According to the error you describe, I’m stumped :?
Maybe you better open-change-save your script using any scriptable text editor…
This is for Smile, and it is very quick!:
property searchFor : "Pepe Leches"
property replaceWith : "KAKA FUTIS"
on open filelist
tell application "Smile" to activate
repeat with i in filelist
updateIt(i)
end repeat
end open
to updateIt(i)
tell application "Smile"
open i
set contents of window 1 to (change searchFor into replaceWith in (contents of window 1 as text))
check syntax window 1
close window 1 saving yes
end tell
end updateIt
Don’t forget adjusting “searchFor” and “replaceWith”!
set oldContents to (contents of window 1)
set applescript's text item delimiters to searchFor
set oldContents to oldContents's text items
set applescript's text item delimiters to replaceWith
set newContents to "" & oldContents
set applescript's text item delimiters to {""}
set contents of window 1 to newContents
thanks a lot jj
you did great job for me
I think you too spent whole day with me to solve this problem.
thanks for time.
I would have never solve this problem with you
thanks once again