Applescript & GarageSale Find & Replace on Selected Items - applescript

I found this script for search and find in GS. Its from 2008 so it was likely wirtten for the earlier version 6 as opposed to the current version 7. Its suppose to find text in the description of any current selected template and change it to whatever the user types it. It displays the search and replace boxes correctly and runs without any errors; however, no text is changed.
I have tried the publisher's forum & documentation; macscripter; and Apple's forum. I have tried modifying it multiple times however these changes produce errors that prevent it from running or it runs but does not do anything.
I am using GS 7.0.16 on OS 10.13.6.
The code appears to originate at this
https://macscripter.net/viewtopic.php?id=26963
Any idea of how to modify it?
set theReply1 to (display dialog "Enter text to replace" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set origtext to text returned of theReply1
set theReply2 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set replacetext to text returned of theReply2
to searchReplace(thisText, searchTerm, replacement)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replacement
set thisText to "" & thisText
set AppleScript's text item delimiters to {""}
return thisText
end searchReplace
tell application "GarageSale"
repeat with aTemplate in every selected template
set origline to get the description of aTemplate
set the description of aTemplate to my searchReplace(origline, origtext, replacetext)
end repeat
end tell
UPDATE:
I downloaded the latest beta of version 8 here:
enter link description here
The above code was written for 6. The publisher apparently made a lot of changes to the scripting language from 6 to 7. The publisher includes documentation on its website for the current 7 and beta 8. These more closely match so I decided to import my data to 8 and use Applescript with that.
I modified the above code to that below by exmining some examples in the beta 8 documentation. This script will find a replace a single continous line of text. You must switch from the Preview mode to the Edit mode and copy the html you want to replace. It will not work by copying the styled text form the Preview mode.
The publisher finally added a bulk find and replace to beta 8, but its currently is having problems with finding strings with html in it.
You can use the built in search of the latest beta 8 to find "This is some text". but it will not find "This is some text""More text". The above script will fidn this text.
The script below also works in GS 7.0.16. Simply load into Apple's script editor and compile as an application on your desktop. Select one listing in GS or one or more folders full of listings and run the compiled script. If I am replacing a large string with another large string I find it helpful to place these into two new TextEdit documents so that I can easily copy and paste them into the two dialog boxes shown by the script.
set theReply1 to (display dialog "Enter text to replace" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set origtext to (text returned of theReply1)
set theReply2 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set replaceText to text returned of theReply2
to searchReplace(thisText, searchTerm, replacement)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replacement
set thisText to "" & thisText
set AppleScript's text item delimiters to {""}
return thisText
end searchReplace
tell application "GarageSale"
repeat with theListing in (get selected ebay listings)
set origline to get the description of theListing
set the description of theListing to my searchReplace(origline, origtext, replaceText)
end repeat
end tell

Related

How to search itunes store for app with applescript?

How to search itunes store for app with applescript? I have been stuck on this and recording doesn't work in itunes.
I haven’t seen recording work in years. In this case, though, I’m not sure it could. While iTunes is scriptable, it doesn’t appear to have any direct means to perform a search of the Stores.
However, there is a means to perform a search using a URL and “open location”. It would look something like:
tell application "iTunes"
open location "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&country=US&media=software&term=ia%20Writer"
end tell
Put the text you want to search for after &term=; here, that’s ia%20Writer (with the %20 meaning a space).
This combines info from Ars Technica and Doug’s AppleScripts.
Here's a way you can use an AppleScript display dialog to enter what App to search for in the iTunes Store using the answer provided by Jerry Stratton:
If the search has spaces in it, the script replaces it with %20 for you.
set theAppSearch to ""
set theAppSearch to text returned of (display dialog "App Search iTunes Store for:" default answer "" buttons {"Cancel", "OK"} default button 2)
if theAppSearch is not "" then
set theAppSearch to my replaceSpaces(theAppSearch)
tell application "iTunes"
activate
open location "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&country=US&media=software&term=" & theAppSearch
end tell
end if
on replaceSpaces(theString)
set AppleScript's text item delimiters to {" "}
set theString to text items of theString
set AppleScript's text item delimiters to {"%20"}
set theString to theString as text
set AppleScript's text item delimiters to {""}
return theString
end replaceSpaces

Replacing words in Applescript

I'm back to writing Applescripts. I'm making a script that will Google things for you. Basically, I need to replace all of the spaces with %20. I know a little about text item delimiters, but I don't know how to implement them in this case.
Here's what I got so far:
if userInput contains "Google " then set {TID, text item delimiters} to {text item delimiters, {"Google "}}
if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
if userInput contains "Google " then set text item delimiters to TID
set openPage to (resultString as string)
if userInput contains "Google " then do shell script "open http://www.google.com/search?q=" & openPage
FYI, the userInput variable is the variable I use when dealing with textboxes.
Thanks
I think this script demonstrates the principle of what you’re trying to do:
tell application "Safari"
activate
display dialog "Search Google for:" default answer "" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
if the button returned of the result is equal to "OK" then
set theSearch to the text returned of the result
set AppleScript's text item delimiters to space
set theSearchList to every text item of theSearch
set AppleScript's text item delimiters to "+"
set theQuery to theSearchList as text
set AppleScript's text item delimiters to ""
set theLink to "http://www.google.com/search?hl=en&q=" & theQuery & "&btnG=Google+Search"
open location theLink
end if
end tell
However, it’s a lot easier if you just ask an app like BBEdit (or its free version, TextWrangler, which has the same AppleScript dictionary) to do the find and replace for you, because BBEdit is an expert at finding and replacing text, whereas AppleScript by itself is not. AppleScript is a “little language” — it deliberately lacks 95% of the functionality of most programming languages because the functionality is in the apps that you command with AppleScript. In other words, AppleScript expects you to have a text editor with which to process text, therefore AppleScript deliberately doesn’t have the built-in text processing ability of a language like Perl (which you can also use in your AppleScripts.)
So the above script gets a lot shorter, easier to write, easier to read, and easier to understand if you add BBEdit (or the free TextWrangler) into the mix:
tell application "Safari"
activate
display dialog "Search Google for:" default answer "" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
if the button returned of the result is equal to "OK" then
set theSearch to the text returned of the result
tell application "BBEdit"
set theQuery to replace " " using "+" searchingString theSearch
end tell
set theLink to "http://www.google.com/search?hl=en&q=" & theQuery & "&btnG=Google+Search"
open location theLink
end if
end tell
Also, notice that you don’t have to run a shell script to open a link in a browser. So this line in your script:
if userInput contains "Google " then do shell script "open http://www.google.com/search?q=" & openPage
… can be written like this:
if userInput contains "Google " then open location "http://www.google.com/search?q=" & openPage
… or like this:
if userInput contains "Google " then
open location "http://www.google.com/search?q=" & openPage
end if
… and you can use the “open location” command with any app, not just Safari. If you tell Finder to “open location” the link will open in your default browser, which might be Chrome.

How to open a folder containing a specific string? (Mac)

We got some folders with project IDs with the following Pattern: x123_projectname.
I use Alfred in my workflow and I need to find and open a specific folder by the ID.
It is possible to run applescripts from Alfred. I'm new to applescript and google helped me to create this this:
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of
(display dialog "What is the ID?" default answer "")
tell application "Finder"
activate
set x to theString & "theID" as alias
open x
end tell
but it didn't work - do you have any hints for me?
You have a couple problems with your script but in general you have the right idea. The main problem is that applescript doesn't work with slash delimited paths. Applescript paths are colon delimited. You can convert from slash to colon delimited using the "posix file" command. Give this a try. Good luck.
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of result
set macString to POSIX file theString
tell application "Finder"
activate
set theResult to every folder of macString whose name contains theID
open theResult
end tell

AppleScript inserts string into application inbetween double quotes

I just purchased Alfred App for my Mac and I want to use this script I found online:
---------------------------------------------------
--Modified by: Pontus Sundén, http://psu.se
--Icon from: http://findicons.com/pack/1362/private_eye_act_1
---------------------------------------------------
on alfred_script(strQuery)
--Get the parameters passed to the script - this is the search query
set strSearchCriteria to SpaceList(strQuery)
--Try to populated an existing window with the search query
tell application "Evernote"
try
set query string of window 1 to strSearchCriteria
on error
--No existing window, open an new one
open collection window with query string strSearchCriteria
end try
end tell
tell application "System Events" to set frontmost of process "Evernote" to true
end alfred_script
--Take a list of text items and retrun them as a string with a space between each item
on SpaceList(astrItems)
--Store what the current list delimiter is
set tmpDelimiters to AppleScript's text item delimiters
--Set the list delimiter to a space and build the string we want to pass back
set AppleScript's text item delimiters to " "
set strReturn to astrItems as string
--Set the list delimiter back to what it was previously
set AppleScript's text item delimiters to tmpDelimiters
--Return the string we built
return strReturn
end SpaceList
which should open up evernote and search for something. It works fine, but instead of searching for, say the word boat, it will search for "boat" with the double quotes and obviously this yields no matches.
Your script is perfectly correct – the spurious quoting of search terms passed via AppleScript is a known Evernote bug in version 3 of the client (well, “known” as in “I opened a support ticket for it a while ago, and Evernote acknowledged it”; I’d add a link to the ticket, but these are private to the user who opened it … will update on progress, though).
Until they get around to fix it, you will have to either use the suggested GUI Scripting solution as a workaround, or correct the search strings manually.
You can use UI scripting to populate the search field like this:
set xxx to "boat"
activate application "Evernote"
tell application "System Events" to tell process "Evernote"
set value of text field 1 of group 4 of tool bar 1 of window 1 to xxx
end tell

Get the current adobe reader window title in applescript

I wrote the following snippet to get the title of the firefox window,
tell application "Firefox"
set window_name to name of front window
display dialog window_name
end tell
it's working well, but when I change firefox to adobe, I get the following error
"Adobe Reader got an error: Can’t get name of window 1."
Any one knows how to get the window title?
You kind of wrote the answer in the question!
tell application "System Events" to set adobe_windows to (get the title of every window of every process whose name contains "Adobe") as list
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to {", "}
set adobe_windows to adobe_windows as string
display dialog adobe_windows
set AppleScript's text item delimiters to prevTIDs
When I get errors referring to window titles, I go to System Events for help. This even applies to the Finder! System Events can do everything the Finder can do, and sometimes more. If you have any questions, just ask. :)

Resources