I have a script returning the following results:
https://XY.com/shop/General-Mad-Dog-Mattis-For-PrN
https://XY.com/search?q=mad%20dog&p=29
https://XY.com/search?q=mad%20dog&p=26
Now i need to find the highest integer (in this case &p=29) and create new strings from https://XY.com/search?q=mad%20dog&p=0 up to the highest found integer in this case https://XY.com/search?q=mad%20dog&p=29
So far i managed to extract the URL's needed with the following code:
set AllUrls to {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29", "https://XY.com/shop/General-Mad-Dog-Mattis-For-PrN"}
-- FILTER PAGING URLS
set PagingFilter to "&p="
set PagingUrls to {}
repeat with i from 1 to length of AllUrls
if item i of AllUrls contains PagingFilter then
set end of PagingUrls to item i of AllUrls
end if
end repeat
PagingUrls -- returns {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29"}
And a small Script to extract the last 2 Digits from the URLs:
set alphabet to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set myURLs to {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29"}
set the text item delimiters of AppleScript to ¬
{space} & characters of the alphabet & {".", "_"}
set a to text items of myURLs as text
get last word of a --> returns "21"
set numlist to {}
repeat with i from 1 to count of words in a
set this_item to word i of a
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
numlist -- returns {26, 29}
This is another approach.
It splits the URLs with text item delimiters by delimiter &p=. If the delimiter exists, get the integer (the right side of the delimiter) and save it as maxValue if the current value is higher than the previous value.
Then use a loop to create the list of page URLs
set AllUrls to {"https://XY.com/search?q=mad%20dog&p=26", "https://XY.com/search?q=mad%20dog&p=29", "https://XY.com/shop/General-Mad-Dog-Mattis-For-PrN"}
set maxValue to 0
set baseURL to ""
set TID to text item delimiters
set text item delimiters to "&p="
repeat with anURL in AllUrls
set textItems to text items of anURL
if (count textItems) is 2 then
set currentValue to item 2 of textItems as integer
if currentValue > maxValue then set maxValue to currentValue
set baseURL to item 1 of textItems & "&p="
end if
end repeat
set text item delimiters to TID
set pageURLs to {}
repeat with i from 0 to maxValue
set end of pageURLs to baseURL & i
end repeat
Related
I have to adjust a lot of files to remove the last part of them:
From this:
108595-1121_gemd_u65_stpetenowopen_em_f_2021-12-03T161809.511773.zip
To this:
108595-1121_gemd_u65_stpetenowopen_em_f.zip
It's always 24 characters that need to be stripped and there is always an underscore at the beginning. The rest is random numbers and characters. I found code below to remove numbers, but I need characters.
My goal is to put this in an automator script with some other processes, but the Renamer in Automator isn't robust enough.
How can I have it strip X-number of characters?
on run {input, parameters}
repeat with thisFile in input
tell application "Finder"
set {theName, theExtension} to {name, name extension} of thisFile
if theExtension is in {missing value, ""} then
set theExtension to ""
else
set theExtension to "." & theExtension
end if
set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers
set name of thisFile to theName & theExtension
end tell
end repeat
return input
end run
No need to use do shell script here, which just confuses the issue. Since your names are underscore-delimited, just use AppleScript's text item delimiters:
repeat with thisFile in input
tell application "Finder"
set {theName, theExtension} to {name, name extension} of thisFile
set tid to my text item delimiters
set my text item delimiters to "_"
set nameParts to text items of theName
set revisedNameParts to items 1 through -2 of nameParts
set newName to revisedNameParts as text
set my text item delimiters to tid
if theExtension is not in {missing value, ""} then
set newName to newName & "." & theExtension
end if
set name of thisFile to newName
end tell
end repeat
return input
What this does, in words:
lines 4 and 5 first save the current text item delimiter (TID) value and then set it to '_'
line 6 breaks the name-string into a list of string parts by cutting the name string at the character '_'
line 7 drops the last item in that list (which is everything after the last '_')
line 8 reverses the process, combining the shortened list of text items into a single string by joining them with '_'
The remainder resets the TID value to its original state, adds the extension to the string, and changes the file name
This code gets two bits of two and copies them to the clipboard.
One bit of text is html. Before stripping out all the html I want to replace the tags with empty lines.
I can't get the replaced correctly, but the rest of the code works.
Any idea what I am doing wrong. I also tried using return instead of linefeed.
tell application "GarageSale 7"
repeat with theListing in (get selected ebay listings)
set des to get the description of theListing
set comment to get private comment of theListing
end repeat
end tell
set theText to des
to searchReplace(thisText, "<br>", linefeed)
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
on removeMarkupFromText(theText)
set tagDetected to false
set theCleanText to ""
repeat with a from 1 to length of theText
set theCurrentCharacter to character a of theText
if theCurrentCharacter is "<" then
set tagDetected to true
else if theCurrentCharacter is ">" then
set tagDetected to false
else if tagDetected is false then
set theCleanText to theCleanText & theCurrentCharacter as string
end if
end repeat
return theCleanText
end removeMarkupFromText
get the clipboard
set the clipboard to removeMarkupFromText(theText) & comment
You've defined a handler called searchReplace(), but you never actually use it. That's why your script isn't replacing the <br> tags.
Firstly, you want to define the handler properly. It should take arguments that are represented by variables; currently, your last two arguments are specific values:
to searchReplace(thisText, "<br>", linefeed)
Here's a suggested edit:
to searchReplace(thisText, searchTerm, replacement)
set my text item delimiters to searchTerm
set thisText to thisText's text items
set my text item delimiters to replacement
set thisText to thisText as text
set my text item delimiters to {""}
return thisText
end searchReplace
Then you can call it from your script like so:
tell application "GarageSale 7"
repeat with theListing in (get selected ebay listings)
set des to get the description of theListing
set comment to get private comment of theListing
end repeat
end tell
set theText to searchReplace(des, "<br>", linefeed)
set the clipboard to removeMarkupFromText(theText) & comment
I'm using this AppleScript to get the content of a selection of eMails:
using terms from application "Mail"
on run {input, parameters}
set mailContents to {}
repeat with aMessage in input
set end of mailContents to content of aMessage
end repeat
return mailContents
end run
end using terms from
The result looks like this:
{"

Here's some text followed by a longer URL, which is cut by a line break!
http://www.XYZ.net/diario/actualidad/economia/20140714/-ciudad-bar
ata-para-el-turismo_353_34541.html
"}
I want to use all links in those specific mails for fort her processing but it's not possible to use the divided URLs as seen in the example before.
So how to tell the AppleScript to keep the URL together?
Try this. I started with your mailContents example. You end up with a list of the links in the httpLinks variable. This works assuming every link starts with "http" and ends with "html". Good luck.
set mailContents to {"

Here's some text followed by a longer URL, which is cut by a line break!
http://www.XYZ.net/diario/actualidad/economia/20140714/-ciudad-bar
ata-para-el-turismo_353_34541.html
"}
set httpLinks to {}
repeat with i from 1 to count of mailContents
set thisContent to item i of mailContents
-- remove all return characters (mac, unix, and windows characters)
set AppleScript's text item delimiters to {character id 10, character id 13, character id 13 & character id 10}
set textItems to text items of thisContent
set AppleScript's text item delimiters to ""
set newText to textItems as text
-- find links
set AppleScript's text item delimiters to "http"
set textItems to text items of newText
if (count of textItems) is greater than 1 then
set AppleScript's text item delimiters to "html"
repeat with j from 2 to count of textItems
set linkItems to text items of (item j of textItems)
set thisLink to "http" & item 1 of linkItems & "html"
set end of httpLinks to thisLink
end repeat
end if
set AppleScript's text item delimiters to ""
end repeat
return httpLinks
This does not break lines, hope you can use it somehow:
tell application "Mail"
set mailSelection to selection
set mailContents to {}
repeat with mail in mailSelection
set end of mailContents to content of mail
end repeat
get mailContents
end tell
Thank you very much! I had to customize your script a bit because not every URL ends with html. Furthermore I used the source of the messages instead of content, because the deleted line breaks had the result, that it was very difficult to separate the URLs from other text. There was no space between them. This is my actual code:
`using terms from application "Mail"
on run {input, parameters}
set mailContents to {}
repeat with aMessage in input
set end of mailContents to source of aMessage
end repeat
set httpLinks to {}
repeat with i from 1 to count of mailContents
set thisContent to item i of mailContents
-- remove all return characters (mac, unix, and windows characters)
set AppleScript's text item delimiters to {"=09", "=" & character id 10, "=" & character id 13, "3D", character id 92}
set textItems to text items of thisContent
set AppleScript's text item delimiters to ""
set newText to textItems as rich text
-- find links
set AppleScript's text item delimiters to "http"
set textItems to text items of newText
if (count of textItems) is greater than 1 then
set AppleScript's text item delimiters to "target"
repeat with j from 2 to count of textItems
set linkItems to text items of (item j of textItems)
set thisLink to "http" & item 1 of linkItems & "target"
set end of httpLinks to thisLink
end repeat
end if
set AppleScript's text item delimiters to ""
end repeat
set list1 to httpLinks
set list2 to {}
repeat with x from 1 to count of items of list1
set n to item x of list1
if n is not in list2 then set end of list2 to n
end repeat
return list2
end run
end using terms from`
I am trying to make an Applescript that condenses multiple lines down to one line.
For example
"a
b
c
d"
to
('a','b','c','d')
The string is in TextWrangler, and my script is
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
tell application "TextWrangler"
tell text window 1
set i to 2
set selection to "('
"
repeat while i < 6
select (insertion point before line i)
select line the (startLine of the selection)
copy (contents of the selection) as text to myText
set newText to text items of myText
set AppleScript's text item delimiters to {""}
set newText to newText as text
select insertion point after line 1
if i = 2 then
set selection to newText
else
set selection to "','" & newText
end if
set i to i + 1
end repeat
set selection to "')"
end tell
end tell
The issue is that result of the script is ('a','b','b'), so I am unable to remove 'new line' character. If someone help me to improve it to work with dynamic number of lines, I will be grateful.
To get the text from TextWrangler, you would use:
tell application "TextWrangler"
set myString to text of window 1
end tell
To update the text in TextWrangler, you use:
tell application "TextWrangler"
set text of window 1 to myString
end tell
So, you can wrap those two pieces of code around adayzdone's code.
tell application "TextWrangler"
set myString to text of window 1
end tell
set myList to paragraphs of myString
set listCount to count myList
repeat with i from 1 to my listCount
set item i of myList to quoted form of item i of myList
end repeat
set {TID, text item delimiters} to {text item delimiters, ", "}
set newString to ("(" & myList as text) & ")"
set text item delimiters to TID
tell application "TextWrangler"
set text of window 1 to myString
end tell
You can try something like this:
set myString to "a
b
c
d"
set myList to paragraphs of myString
set listCount to count myList
repeat with i from 1 to my listCount
set item i of myList to quoted form of item i of myList
end repeat
set {TID, text item delimiters} to {text item delimiters, ", "}
set newString to ("(" & myList as text) & ")"
set text item delimiters to TID
return newString
I was wondering if anyone could help me. I'm trying to find and replace blank spaces in an excel workbook. I want it to search the current sheet I'm on in a workbook, find all instances of "" and replace it with " ".
Does anyone happen to know how to do this?
With Applescript:
searchAndReplaceTextInCells("hello", "world")
on searchAndReplaceTextInCells(search_str, replace_str)
tell application "Microsoft Excel"
set search_range to range "A:Z"
set all_found_ranges to {} -- store for the ranges, to manipulate after searching
set found_range to ""
set counter to 0
try
set found_range to find search_range what search_str with match case
on error
log ("No matches found")
end try
if (found_range is not "") then
set first_cell_address to (get address of the cells of found_range) -- we use this to break our loop
repeat while true
set counter to counter + 1
copy found_range to end of all_found_ranges
-- Now look for next result
set found_range to find next search_range after found_range
set cell_address to (get address of the cells of found_range)
if (cell_address = first_cell_address) then
-- have looped around so we are finished!
exit repeat
end if
end repeat
end if
-- walk all the ranges found and do the string replacing
repeat with r in all_found_ranges
set value of r to my replace_chars(the value of r, search_str, replace_str)
end repeat
log ("found and replaced " & counter & " items")
end tell
end searchAndReplaceTextInCells
on replace_chars(this_text, search_string, replacement_string)
set my text item delimiters to the search_string
set the item_list to every text item of this_text
set my text item delimiters to the replacement_string
set this_text to the item_list as string
set my text item delimiters to ""
return this_text
end replace_chars
How about Command + F, and hit the Replace button to give you this:
Have you tried the following:
replace range targetRange what searchStr replacement replaceStr
This should be rather straight forward replacement statement.