I would like to find a safari tab, and focus on this tab
This is not working, however the tab is found alright, I just can't open this tab.
tell application "Safari"
repeat with t in tabs of windows
tell t
if name starts with "facebook" then open tab
end tell
end repeat
end tell
Also I wonder can I found some text from a another tab without focus on this tab ?
to getInputByClass75(theClass, num)
tell application "Safari"
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return input
end getInputByClass75
getInputByClass75("Order", 0)
set theText to Unicode text
set theSource to getInputByClass75("Order", 0)
property leftEdge75 : "<a class=\"columns\" href=\"/Web"
property rightEdge75 : "\">Display</a>"
set saveTID to text item delimiters
set text item delimiters to leftEdge75
set classValue to text item 2 of theSource
set text item delimiters to rightEdge75
set OrderLink to text item 1 of classValue
set text item delimiters to saveTID
OrderLink
The right phrase to bring a tab to foreground is
tell window x to set current tab to tab y
Try this
tell application "Safari"
repeat with w in windows
if name of w is not "" then --in case of zombie windows
repeat with t in tabs of w
if name of t starts with "facebook" then
tell w to set current tab to t
set index of w to 1
end if
end repeat
end if
end repeat
end tell
--
You can execute javascripts in background tabs. Specify the tab like this:
tell application "Safari"
do JavaScript "document....." in tab 1 of window 1
end tell
Related
I have problem with filename tabs checking. I have openfiledialog, when i click open C:\test.txt file, first tab on tabcontrol is created, when i open second one C:\test2.text open's ok, but when i open again the first one it doesn't activates the tab beacause it's already in tab, it creates new tab with this filename or doubles it. I use checking file name with tab.tag so CreateTab Sub has filename fullpath in tab.tag.
Here is my code:
Select Case QTabControl.Controls.Count
Case = 0
If ofdMain.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
CreateTab(ofdMain.FileName)
End If
Case Else
If ofdMain.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
For Each tab As QTabPage In QTabControl.Controls
If tab.Tag = ofdMain.FileName = False Then
CreateTab(ofdMain.FileName)
Else
QTabControl.ActivateTabPage(tab)
End If
Next
End If
End Select
and after opening 7 tabs, only first tab gets activate if i opened it again, but not other tabs just duplicates opening.
everybody, I am trying to make the user enter the name of the movie they want subtitles for and the for apple script to automatically search the subtitle website for the name of the movie they inputted.
To do this all of the spaces in the movie name need to be replaced to the + sign because urls convert spaces to the + sign. The code is not working an im getting the following errors:
Expected “end” but found “on”.
A “(” can’t go after this identifier.
Here is my code;
on run
display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
set moviename to text returned of the result
set theText to moviename
set theSearchString to " "
set theReplacmentString to "+"
end findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
goToWebPage("https://rs.titlovi.com/prevodi/?prevod= & thetext")
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end goToWebPage
end run
Thank you in advance.
Functions (handlers, in AppleScript speak) may not be nested in AppleScript. So you need to either move findAndReplaceInText and goToWebPage outside of on run, or merge their functionality into on run without using handlers.
Handlers begin with on handlerName and end with end handlerName; you have findAndReplaceInText beginning and ending with end findAndReplaceInText.
Here’s how it might work after separating the handlers:
on run
display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
set moviename to text returned of the result
set moviename to findAndReplaceInText(moviename, " ", "+")
goToWebPage("https://rs.titlovi.com/prevodi/?prevod=" & moviename)
end run
on findAndReplaceInText(thetext, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of thetext
set AppleScript's text item delimiters to theReplacementString
set thetext to theTextItems as string
set AppleScript's text item delimiters to ""
return thetext
end findAndReplaceInText
on goToWebPage(theWebPage)
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end goToWebPage
I’ve verified this code in Safari on Mac OS X 10.14.6.
The goal of this script is to:
Ask the user how many text replacement (System Preferences>Keyboard>Text) shortcuts they'd like to have. The text returned is set to my variable "gTextReplacementNum" as number. See my second handler "HowMany()"
Have the user provide the text replacement shortcut and the text to do the replacing for the number of shortcuts they wanted. See my third handler "GetText()"
Take the user provided text contained in a variable to create a new AppleScript doc that does all the heavy lifting for them. Code not yet written; not within scope of question.
Then they have a personalized AppleScript Application Bundle they may launch on their Mac to auto-populate the text replacement preferences pane.
I am having trouble getting this to work properly. I need the loop to keep adding the answers to a variable as a list or to a variable that increments its name according to the loop instance (e.g. TextReturned_i, TextReturned_i+1, etc).
Have I adequately explain this?
global gTextReplacementNum
set gTextReplacementNum to 0
# Main Logic Begins
try
Start()
HowMany()
GetText()
on error errText number errNum
display alert "Error " & errNum message errText
end try
# Main Logic Ends
# Handlers Begin
-- First Handler
on Start()
display alert "Automated Text Replacement v1.0" message "Created by: Me
myemail#domain.com" buttons {} giving up after 4
display alert "About" message "This app will have you provide a text 'short cut' to replace with and replacement text. It then compiles all this into an application that can be run on any Mac.
Would you like to continue?" buttons {"No", "Yes"} cancel button 1 default button 2
end Start
-- Second Handler
on HowMany()
display dialog "How many text replacement shortcuts would you like?
Please enter numericals only. (1, 2, 3)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
copy the result as list to {ButtonPressed, TextReturned}
set gTextReplacementNum to TextReturned as number
end HowMany
-- Third Handler
on GetText()
repeat with i from 1 to gTextReplacementNum as number
display dialog "What text would you like to replace?
(this is your shortcut)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set TextShortcut to text returned of result as list
display dialog "What is the replacement text?
(this is what the shortcut fills out)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set TextReplaced to text returned of result as list
end repeat
end GetText
# Handlers End
In your GetText() handler, you are replacing the value TextShortcut and TextReplaced each time. You need to
set aList to aList & newValue
to build a list in a repeat loop.
Also, as is, this handler never returns the value of these two lists. So, I'd suggest, using your scheme, make these two variables globals as well.
So, the full changes are:
1. Add to the declarations:
global gTextReplacementNum
global gTextShortcut
global gTextReplaced
set gTextReplacementNum to 0
set gTextShortcut to {}
set gTextReplaced to {}
and 2. edit your GetText() handler:
-- Third Handler
on GetText()
repeat with i from 1 to gTextReplacementNum as number
display dialog "What text would you like to replace?
(this is your shortcut)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set gTextShortcut to gTextShortcut & (text returned of result)
display dialog "What is the replacement text?
(this is what the shortcut fills out)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set gTextReplaced to gTextReplaced & (text returned of result)
end repeat
end GetText
An alternate method would be to read a tab delim file and work from that with a standard script. Something like:
property fileName : "shortcuts.txt"
set filePath to (path to desktop as string) & fileName
set theData to read file filePath
set theRecords to paragraphs of theData
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with thisPair in theRecords
set {theShortcut, theReplacement} to text items of thisPair
setKeyboardPref(theShortcut, theReplacement)
end repeat
set AppleScript's text item delimiters to oldDelim
on setKeyboardPref(theShortcut, theReplacement)
-- set up the pair
display dialog theShortcut & return & theReplacement
end setKeyboardPref
Using telerik and the radmenu, do you know how to check if the item exists by text
my menu contains the text "menu1"
If I use menu.FindItemByText("menu1").Enabled = False this will disable the button
BUT
If I use menu.FindItemByText("menuTEST").Enabled = False then I get an exception as this button does not exist.
How do I stop the error?
I tried this below but it say it cant return booloen
If menu.FindItemByText("menuTEST") then
'do this
End If
Try this:
If Not(menu.FindItemByText("menuTEST") Is Nothing) then
'do this
End If
I made was making this applescript to launch apps with a note:
tell application "Notes"
if exists note starts with applaunch then
set LCommands to {"Launch", "Open"}
repeat with y from 1 to count LCommands
set applaunch to (item y of LCommands)
set AppleScript's text item delimiters to applaunch
set myApp to text items 2 thru 1 of note
set AppleScript's text item delimiters to {""}
set myApp to myApp as text
if y = 1 or y = 2 then
tell application myApp to launch
end if
end repeat
delete note starts with applaunch
end tell
and returns the error "the variable applaunch is not defined" but i defined it. what to do?
You reference applaunch in line 2, but you don't define it until line 5.
Also, your code example is missing the end if that goes with if exists note starts with applaunch then.
You can try something along these lines:
set LCommands to {"Launch ", "Open "}
tell application "Notes"
repeat with aCommand in LCommands
set aCommand to (contents of aCommand)
set myNotes to (notes whose name begins with aCommand)
repeat with aNote in myNotes
set aNote to contents of aNote
set noteName to aNote's name
set AppleScript's text item delimiters to aCommand
set myApp to text items 2 thru -1 of noteName
set AppleScript's text item delimiters to {""}
set myApp to myApp as text
-- If you need to work with the Note's content as plain text
--set noteBody to do shell script "echo " & (quoted form of (aNote's body as text)) & " | textutil -stdin -convert txt -stdout "
my launchDelete(aNote, myApp)
end repeat
end repeat
end tell
on launchDelete(theNote, theApp)
try
tell application theApp to launch
tell application "Notes" to delete theNote
end try
end launchDelete