Replace All Error, Applescript - applescript

I am trying to get this script to click "replace all" using keystrokes.
I am getting an error:
"System Events got an error: Can’t get button \"Replace All\"." number
-1728 from button "Replace All"
tell application "Mail"
set theSenderList to {}
set theMessages to the selected messages of message viewer 0
repeat with aMessage in theMessages
set end of theSenderList to {address of to recipient of aMessage, " OR"}
end repeat
set AppleScript's text item delimiters to " "
set the clipboard to (theSenderList as string)
set AppleScript's text item delimiters to " "
beep
end tell
set clip to (the clipboard as text)
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
set value of cell "a9" to clip
end tell
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
set value of cell "b9" to current date
end tell
tell application "Numbers"
activate
end tell
tell application "System Events"
delay 1.0
keystroke "f" using command down
keystroke "#gmail.com"
keystroke tab
keystroke ""
delay 2
click button "Replace All"
end tell

It appears that you are trying to replace the occurrence of "#gmail.com" in a bunch of email addresses with blanks, thus deleting that portion of the address. You could do that much more easily in plain AppleScript:
set targetAddresses to {"fred#gmail.com", "wilma#gmail.com", "barney#gmail.com"}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "#gmail.com"
set fixedAddresses to {}
repeat with eachAddress in targetAddresses
set end of fixedAddresses to text item 1 of eachAddress
end repeat
set AppleScript's text item delimiters to astid
fixedAddresses
-->{"fred", "wilma", "barney"}
Is this close to what you are trying to achieve?

Related

Why does this Applescript cause "Export Library.scpt: execution error: System Events got an error: Can’t get process "i". (-1728)"?

ƒIt's caused when run through osascript from terminal using the command line "osascript Export\ Library.scpt /Users/bryandunphy/Development/iTunesLibraryConsolidator testing.xml".
same script as my last (solved) question but posting it's entirety this time.
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menuClick(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menuClickRecurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menuClick
on menuClickRecurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menuClickRecurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menuClickRecurse
-- Created by Bryan Dunphy during January of 2017
--
-- select the folder "directory" in Window "WinName" of Application "appName" and then clicks the default button if requested
-- if WinName is "" then it uses the frontmost window (to allow for unnamed windows)
-- REQUIRES "on handleDir" and "on findRoot" to work!
-- ONLY call switchDir
-- "createIt" is a boolean that will create any missing directories if it is set to "true".
-- "selectDefault" is a boolean indicating whether or not to click the window's default button after selecting the specified directory
-- returns "true" or "false" to indicate success.
-- clicks "Cancel" button on failure
-- Always returns "true" if "createIt" is set to "true"
on switchDir(directory, winName, appName, createIt, selectDefault)
local dirs, delim
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set dirs to every text item of directory
tell application "System Events" to tell process appName to set frontmost to true
my findRoot(appName, winName)
repeat with dir in dirs
if not handleDir(dir, winName, createIt) then
tell application "System Events" to tell process appName to tell button "Cancel" to click
return false
end if
end repeat
if selectDefault then keystroke return
return true
end switchDir
on handleDir(dir, winName, appName, createIt)
local foundIt
foundIt = false
local ndx
if winName is not "" then
repeat with ndx from 1 to (count of window winName's list 1)
if window winName's list 1's item ndx's value is equal to dir then
select window winName's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
else
repeat with ndx from 1 to (count of front window's list 1)
if front window's list 1's item ndx's value is equal to dir then
select front window's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
end if
if not foundIt then
if createIt then
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
else
tell application "System Events" to tell process appName to tell its front window
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
end if
end if
else
return foundIt
end if
end handleDir
on findRoot(appName, winName)
local rootName
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
else
tell application "System Events" to tell process appName's front window
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
end if
return rootName
end findRoot
on run (clp)
if clp's length is not 2 then error "Incorrect Parameters"
local destination, libraryName
set destination to clp's item 1
set libraryName to clp's item 2
menuClick("iTunes", "File", "Library", "Export Library…")
set value of parentObject's text field "Save As:" to (libraryName and ".xml")
tell pop up button 1 of group 1 of window "New iTunes Library" of process iTunes of application "System Events" to click
repeat with ndx from 1 to (count of parentObject's menu 1)
if title of menu item ndx is "" then
select menu item (ndx - 1)
exit repeat
end if
end repeat
my switchDir(destination, "iTunes", "iTunes", true, false)
set the value of text field "Save As:" of window "iTunes" to (libraryName + ".xml")
tell button "Save" of window "iTunes" to click
return (destination and "/" and libraryName and ".xml")
end run
Change:
menuClick("iTunes", "File", "Library", "Export Library…")
to:
menuClick({"iTunes", "File", "Library", "Export Library…"})
AppleScript's error reporting is truly awful (no tracebacks, for starters), and osascript is even worse than Script Editor for debugging. If you run the script in SE, it will at least highlight the line in your script where the error occurred. If that doesn't give you a clue, add log commands to report the script's progress. osascript will write logged messages to stderr. In SE, click the awful 'document' (show/hide log) icon at the bottom of the window, then select 'Messages'. If your time is worth more than $100, get yourself a copy of Script Debugger which also lets you add breakpoints and step through and inspect variables as the script runs.

Filename without extension in AppleScript

How to get the filename without the extension in AppleScript? For example:
Penny.Dreadful.S03E01.720p.HDTV.x264-BATV.mp4 to
Penny.Dreadful.S03E01.720p.HDTV.x264-BATV
One file to work enough for me but could be in for more file.
The following code takes the file name but with the extension. I don't want the extension. Thanks for your help.
try
set theNames to {}
tell application "Finder"
repeat with i in (get selection)
set end of theNames to name of i
end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
end try
Try this, instead of the try block the script checks for empty selection and the fileName is extracted by subtracting the extension if there is one.
set theNames to {}
tell application "Finder"
set theSelection to selection
if theSelection is {} then return
repeat with anItem in theSelection
set {name:fileName, name extension:fileExtension} to anItem
set end of theNames to my removeExtension(fileName, fileExtension)
end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
on removeExtension(Nm, Ex)
if Ex is missing value or Ex is "" then return Nm
return text 1 thru ((count Nm) - (count Ex) - 1) of Nm
end removeExtension

applescript to open google search query in youtube

I am using the code below to open youtube with google query in applescript as the new google does not carry my search string to youtube.Unable to open url in firefix with the google search string in firefox. I really appreciate any help.Thanks in advance.
I am getting an error here :
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
set tr to get the clipboard
end tell
return trimgoogle(tr)
on trimgoogle(sourceAddress)
set AppleScript's text item delimiters to {"#"}
set addressParts to (every text item in sourceAddress) as list
set AppleScript's text item delimiters to ""
set nameOnly to item 2 of addressParts
set AppleScript's text item delimiters to {"&"}
set addressParts2 to (every text item in nameOnly) as list
set AppleScript's text item delimiters to ""
set nameOnly1 to item 1 of addressParts2
set nameOnly2 to nameOnly1
set AppleScript's text item delimiters to {"="}
set addressParts21 to (every text item in nameOnly2) as list
set AppleScript's text item delimiters to ""
set nameOnly12 to item 2 of addressParts21
set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12
return newurl
end trimgoogle
tell application "Firefox"
OpenURL newurl
end tell
Try this:
tell application "Firefox"
open location newurl
end tell
EDIT:
Firefox does not activate itself automatically, so I just tried this:
tell application "Firefox"
activate
open location "http://www.wikipedia.com/"
end tell
which works here.
Here more about encoding text:
http://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
EDIT2:
OK, I see what you mean, that would be solved like this:
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
set tr to get the clipboard
end tell
set newurl to my trimgoogle(tr)
tell application "Firefox"
activate
open location newurl
end tell
on trimgoogle(sourceAddress)
set AppleScript's text item delimiters to {"#"}
set addressParts to (every text item in sourceAddress) as list
set AppleScript's text item delimiters to ""
set nameOnly to item 2 of addressParts
set AppleScript's text item delimiters to {"&"}
set addressParts2 to (every text item in nameOnly) as list
set AppleScript's text item delimiters to ""
set nameOnly1 to item 1 of addressParts2
set nameOnly2 to nameOnly1
set AppleScript's text item delimiters to {"="}
set addressParts21 to (every text item in nameOnly2) as list
set AppleScript's text item delimiters to ""
set nameOnly12 to item 2 of addressParts21
set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12
return newurl
end trimgoogle

Issue with applescript function

I'm trying to the function replaceChars in the below script but getting the following error:
error "Finder got an error: Can’t continue replace_chars." number -1708
The intention is to add the script to finder as a button so that I can simply click it to copy the path to my clipboard. I'm adding file://localhost/ so that the link can then be used when shared with users by email as a direct link to a folder on the local network. If possible I would also like to add to clipboard the same for Windows machines.
If you could offer any guidance as to the task above it would be much appreciated, this is my first attempt at programming with applescript so I'm not that knowledgable of how things are done.
Heres the code:
on appIsRunning(appName)
tell application "System Events"
set isRunning to ((application processes whose (name is equal to appName)) count)
end tell
if isRunning is greater than 0 then
return true
else
return false
end if
end appIsRunning
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
if appIsRunning("Finder") then
tell application "Finder"
set thePath to "file://localhost/" as text
set theTarget to (target of front Finder window) as text
set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text
end tell
end if
AppleScript is looking for a replace_chars handler in the Finder's scripting dictionary. You can either make it into my replace_chars to have AS look in the script, or (probably better) move the set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text line out of the tell block altogether.
You can make it into a one-line script:
tell application "Finder" to set the clipboard to "file://localhost" & (target of front Finder window as text)'s POSIX path
Replace your replace_chars(theTarget, ":", "/") call with replace_chars(theTarget, ":", "/") of me:
set the clipboard to thePath & replace_chars(theTarget, ":", "/") of me as text

Remove quotes on "quoted form of" in Applescript?

I have the following code to copy paths of multiple selected items in Finder:
activate application "Finder"
tell application "Finder"
set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
set the end of pathList to quoted form of (POSIX path of (anItem as alias))
end repeat
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set the clipboard to pathList as string
set AppleScript's text item delimiters to savedDelimiters
end tell
The only problem is that it results in this:
'/Applications/Burn.app/'
'/Applications/Caffeine.app/'
'/Applications/Calculator.app/'
Thats' basically what I want, but I don't want those damn singlue quotes in there. How do I get rid of them? I already tried just removing quoted form of but without luck.
Thanks!
all you have to do is take out "quoted form of "
activate application "Finder"
tell application "Finder"
set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
set the end of pathList to (POSIX path of (anItem as alias))
end repeat
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set the clipboard to pathList as string
set AppleScript's text item delimiters to savedDelimiters
end tell

Resources