How to google Arabic words with Apple Script - applescript

I am trying to have Apple Script do some google-searches for me. It works perfectly, as long as the word is in Latin letters. As soon as the word is in Arabic, the code doesn't do anything anymore.
Is there anyone who knows how to solve this?
I have tried using the google-URL for an arabic version of Google but that did not help.
set search to "زق"
open location ("https://www.google.de/#q=" & search)
This code doesn't do anything. If I replace the Arabic letters with any latin letters it works.
It's a puzzle to me, because when I just write "display search" it works as well, so the output of a string with Arabic letters works. If anyone knew of a fix to this, it would be amazing!

What works though is this (you have to first allow System Events to make keystrokes in another application in Settings):
open location ("https://www.google.de/#q=")
delay 6
tell application "System Events" to tell process "Script Editor" to keystroke "زق" & return
delay 4

Related

How to change the wallpaper for all spaces in macOS using Applescript/shell?

Before I've written this question, I've reviewed several answers and the most popular ones was
tell application "System Events"
tell every desktop
set picture to "/Users/xyz/Documents/Screenshots/wallpaper.png"
end tell
end tell
But it doesn't seem to be working.
What I've tried?
Steps:
Make sure have more than one space
Run the script
Expected result: All spaces should see new wallpaper
Actual result: Wallpaper is changed to only current space
Goal: Is there a way to change the wallpaper to all the spaces using applescript/shell?

AppleScript insert text into Microsoft Word document

I am new to MacOS standalone application development and I am working on fixing issues in our existing application. Our application access the Microsoft Word document and perform insertion of text into the word document via AppleScript. When i am running the application and performing the insert operation via XCode, the text is not getting inserted into the Word Document. Its been working well earlier but from past few days the text is not getting inserted into Word document
The following code is being used in XCode to copy text to pasteboard and then calling the AppeScript to just insert the text into Word Document
NSPasteboard * pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:#[textToInsert]];
Following is the AppleScript being used for insertion of text into Word Document
on insertSampleText()
try
using terms from application "Microsoft Word"
tell application "Microsoft Word"
activate
tell application "System Events"
keystroke "v" using {command down}
end tell
end tell
end using terms from
return "0"
on error the error_message number the error_number
set the error_text to "" & the error_number
return the error_text
end try
end insertSampleText
Can you please suggest on what needs to be done in order to insert text into Word Document?
The line
keystroke "v" using {command down}
won't work unless the user has enabled the Accessibility Keyboard in control panel.
Assuming that your code is successfully running the on insertSampleText handler (personally I would put some display dialog statements in there to try to check the flow of execution) then I would start by trying something more like
tell application "Microsoft Word"
activate
paste object text object of selection
end tell
but what will work may depend on exactly what you have in the clipboard and what exactly you want to happen to any existing selection
(Although it's not obviously related to your question, if you are having trouble getting your handler code to run at all, you may find the discussion at Using AppleScript with Apple Events in macOS - Script not working helpful)

Is there a way to trigger a Hot Key/Keyboard Shortcut via a Shell Script, a AppleScript or a Automator Workflow?

I'm an avid Keyboard Maestro user and I need a workaround for triggering a keyboard shortcut like ⌘⇧L (externally, without Keyboard Maestro). So I thought a bash script would be capable of doing such a thing. An AppleScript or an Automator workflow would be sufficient, too. I anybody could help me this would be great.
You don't have to read this, but here's why I want to do what I want to do:
I have a the same string assigned to various Markdown macros, I use a string instead of Hotkeys because it's much more memorable for me since my brain already is filled with so many application shortcuts. The disadvantage is that Keyboard Maestro won't delete the keystrokes of the string. I can perform several actions within the program to delete them but adding these actions for each and every macro is tedious and suboptimal.
tell application "System Events" to keystroke "l" using command down & shift down
tell application "System Events"
key code {123, 124} using {shift down, command down} -- ⇧⌘←, ⇧⌘→
keystroke "c" using command down -- keystroke "C" would be treated as ⇧C
end tell
delay 0.02 -- you need a small delay here before the next command
set txt to Unicode text of (the clipboard as record)
Reference of Mac key codes: lri.me/chars

Write a character with Applescript

Really basic, but how to write a character with Applescript. My 'd' key is broken, i'd like to assign a script to a f1-12 key just to write that character in any application. Thank's!
This should do: tell application "System Events" to keystroke "d"
It would be easier if you simply acquired a new keyboard.
Not all applications are scriptable, GUI scripting is unreliable, and sometimes even the simplest of scripts can take multiple seconds to run, so Applescript really isn't a reasonable solution to your broken keyboard problem.

Creating an AppleScript to do a find in a replace in a given document

I'm looking to create aAppleScript that when run will:
Search the document for a given string
Replace that string with another given string
The strings will always be the same
Search for
This will be used in textmate - I was trying to do this in textmate
I know I can use textmate's find and replace functionality - I'm just trying to automate a little.
This should only make changes on the current page.
Is this possible?
UPDATE:
So I've found some code that has got me started...
tell application "TextMate" to activate
tell application "System Events"
keystroke "f" using {command down}
tell process "TextMate"
keystroke "<?"
keystroke tab
keystroke "<?php"
click button "Replace All"
end tell
keystroke "esc"
end tell
but I get the following error:
error "System Events got an error: Can’t get button \"Replace All\" of process \"TextMate\"." number -1728 from button "Replace All" of process "TextMate"
On the find and replace dialog of Textmate the button is labeled "Replace All" Am I missing something here?
You'll have to send the keystroke to the proper window. Something like tell window "find dialog" (or whatever). You have to be completely specific, so it might be
tell tab 1 of pane 1 of window "find and replace" of app textmate...
User interface scripting is so hackalicious you should only do it as a last resort.
Looks like you need sed.
on a command line, or with do shell script:
cat /path/to/your/file.php|sed "s_<?_<?php_g">/path/to/your/newfile.php
or for a whole folder's worth
cd /path/to/your/folder
for file in *.php; do cat "$file"|sed "s_<?_<?php_g">"${file/.php/-new.php}"; done
You're better off looking at MacScripter; there are lots of examples and solutions for find and replacing with or without a texteditor using Applescripts delimiters: MacScripter / Search results, like this:
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
It's all very well sending folks to do search/replace within AppleScript strings, or sending them to Mac OS X's underlying Unix tools, like sed and Perl, but those are often no real substitue for searching/replacing text directly in the target application.
I had the exact same problem, albeit in Dragon Dicate rather than TextMade. Google led me here, where I was dismayed to find no direct solution. So let me share the one I came up with:
set find to "the"
set replace to "THE"
tell application "Dragon Dictate"
activate
tell application "System Events"
tell process "Dragon Dictate"
keystroke "f" using {command down}
keystroke find
keystroke tab
keystroke replace
tell window "Find"
click button "Replace All"
end tell
end tell
end tell
end tell
The key difference is addressing the Find window, which knows about the "Replace All" button. You will also have to change the "Dragon Dicate" target app to "TextMate" of course. (AppleScript seems to require knowing EXACTLY what app a script is being fired against, unless you want to fall back into some truly ugly low-level message sending. When dealing with AppleScript, that's just the 337th sigh of the day!)
If you want to write an AppleScript to manipulate text, there is nothing better than to use an AppleScriptable text editor. That is the right tool for the job. Then you can write just a few lines of code and get the job done.
For example, in TextMate, go Edit > Select All and Edit > Copy to copy the contents of your document to the clipboard. Then run this AppleScript:
tell application "TextWrangler"
activate
set theSearchString to the clipboard
set theResultString to replace "old term" using "new term" searchingString theSearchString
set the clipboard to theResultString
end tell
Then go back into TextMate and go Edit > Paste.
TextWrangler is available for free in Mac App Store.
You may be able to expand this script so that the TextMate work is automated with GUI scripting, but since it is just selecting all and copying and pasting, that is a fairly small task.

Resources