Translate Applescrip [key code 125 using command down] to appscript - applescript

how to translate the following Applescript to appscript:
tell application "System Events"
key code 0 using command down
end tell
I want to perform "Command + A"-like short cut, i.e., select all texts.

Look at the application ASTranslate which was installed as part of Appscript. It translates Applescript to Appscript for Python or Ruby. Be aware it just traps Apple Events and thus won't translate Applescript structures like loops or the like. It's very easy to use. Just past your Applescript in one pane, hit cmd-R, and it'll generate the translated Appscript + Python code. For your example it is
app(u'System Events').key_code(0, using=k.command_down)
While occasionally you'll find something that won't work quite right in general ASTranslate is an essential tool for using Appscript.

Bellow is the usage of keystroke of Applescript via Python, which is hard to search:
http://www.agapow.net/programming/python/applescript-via-python

Related

Find characters in PDF, return whole word

Apologies, my AppleScripting knowledge is very basic, however, I understand general scripting as I have PowerShell knowledge.
I would like a simple line of script that can search an open PDF for characters e.g. "Lon" and return the whole word e.g. "London" as a variable.
If you would like to have a laugh at my pathetic attempt see below:
tell application "Preview"
set newfile to (get word 1 of text of "Lon*")
end tell
thanks in advance
Preview is not well scriptable using basic vanilla AppleScript, however, the following example AppleScript code uses Skim as the PDF Reader, and can do what you are asking:
considering case
tell application "Skim" to ¬
set the targetWord to the first word of ¬
the text of the front document where it starts with "Lon"
end considering
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

How to get properties of an application

I want to get the properties of an Application (the name is given as command-line argument). Basically I want to know if the application_name in command-line argument is front-most or not.
This is what I have been doing (It is working for me)
tell application "Google Chrome"
get properties
end tell
But when I try to do this:
on run argv
tell application (item 1 of argv)
get properties
end tell
end run
I'm getting an error when executing it with command osascript has_focus.scpt "Google Chrome"
environment/mac/scripts/has_focus.scpt:56:66: execution error: Google
Chrome got an error: Can’t get every property. (-1728)
Please help.
I see that you solved your problem through the discussion, and learned about the frontmost property for application objects.
So this is more of a point of interest to complete what you originally started.
#vadian was correct by stating:
The argument of tell application must be a (literal) constant because the terminology is evaluated at compile time.
The solution around this is to not utilise terminology. Terminology requires that the application being instructed be able to lookup the terminologies used in its scripting dictionary, and convert them from human-friendly terms to raw Apple event codes. So take away the middle man, and you can issue statements, give commands, and retrieve properties by way of raw chevron syntax:
on run argv
set [appName] to argv
tell the application named appName ¬
to return its «class pALL»
end run
Then, in a terminal:
osascript ~/Scripts/getAppProperties.applescript "Brave Browser"
«class ChBB»:«class CrBF» id 1, frontmost:false, «class ChOB»:«class CrBF» id 2,
class:application, name:Brave Browser, version:83.1.10.97
Naturally, with no dictionary lookup, the application-specific properties and their values are returned in raw syntax.
Note: Entering raw syntax codes into Script Editor can be a bit tricky, as it will immediately compile and any terms that are understood either by Script Editor or AppleScript (such as properties, which features commonly across the board and with the same type code). But you can create an AppleScript in any plain text editor. If you really must compile it into a .scpt file, then osacompile will do that.
This cannot work.
The argument of tell application must be a (literal) constant because the terminology is evaluated at compile time.
An exception are the properties version, frontmost and running which any application retrieves. These properties are internally organized as Application Object and independent of an existing AppleScript dictionary.

Scripting itunes without applescript?

I am looking for a way of scripting iTunes using something else then AppleScript. I want to mass-manipulate the title-name and artist of tracks. Using some id3-tool won't help, because as far as I know the iTunes database will not update, if I don't use iTunes for manipulating this information. As I don't know how to code with AppleScript and don't really have the time to dive into this, I wonder: is there any way to do this task using javascript, lua, bash or php?
thanks,
I suggest you to learn AppleScript, at least for a day. The point is, the inter-GUI-app communication in OS X is done via Apple Events, whose construct follows that of its main language AppleScript. There are many bridges which allow you to call Apple Events from various languages, but you need to understand the concept of Apple Events first. So, at least you need a bit of familiarity with AppleScript.
This is just as in the case of Cocoa: you can code Cocoa apps in many languages, but most of the documentation and the concept are based on Objective-C. So you need to at least a bit of familiarity with Objective-C before coding Cocoa in other languages.
So, let me give you a very very short overview of Apple Events/ AppleScript system.
Each app implements an object-oriented system and exposes it to the outside world as a dictionary, which you can read with AppleScript Editor. Open the AppleScript Editor, and choose File → Open Dictionary, and choose iTunes. There, you see the list of commands, classes, methods in those classes, etc. Then, from AppleScript or Ruby or Lua, you access these objects and methods.
Suppose you want to rename the selected entries of iTunes from "A-B" to "B-A". Then the code would be
set text item delimiters to "-"
tell application "iTunes" -- following statements are targeted to iTunes
repeat with entry in selection -- "selection" is a concept implemented in iTunes
set s to name of entry -- copy the name of entry to a local string s
set x to text items of s -- split the string s to a list according to text item delimiters
set y to {item 2 of x, item 1 of x} -- construct another list
set name of entry to y as string -- set the name. Note that "as string" adds the delimiters
end repeat
end tell
Yes the grammar of AppleScript is a bit weird, but it basically has one-to-one correspondence with a regular imperative language. Just refer to the official language guide if you're confused. And text operations in AppleScript without a good OSAX (AppleScript's extension system) is a chore. So I agree it's not a bad idea to first get familiar with the concept of AppleScript, and then use it from Ruby or any of your favorite languages.
But remember, open the dictionary in AppleScript editor, because that's where you find what each app implements and exposes to the system!
You can send Apple Events from JavaScript with JavaScriptOSA. However I'd recommend you investigate appscript (Python, Ruby) instead as it's more up-to-date and supported.

What are the advantages of using vim to program Ruby (over Notepad++)? (other languages, too, but specifically Ruby) [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I've been using Notepad++ for a while; in fact, I've even started using Launchy for that "load this resource into the editor right now" functionality that many fuller IDE's like Eclipse has. It has syntax highlighting, split window view, code collapsing, parentheses (and other delimiter) paring, automatic indent, block commenting.
However, it seems that everywhere I go, people are using vim for their programming needs.
I know the basics of vim; it's my main "basic text editor" when I'm on a linux machine; I use it like I'd use the basic Notepad on Windows. I get the controls, most of the shortcuts, the repetition eliminators, etc.
What I'm not quite understanding are these killer apps that people are espousing about. How "debugging Ruby in vim was a life-changing experience", or how, when using it, magic occurs.
Am I missing some essential plug-ins? Is there a dimension I'm not seeing? Should I just shut up and start using it for a bit, to see? How do I get syntax highlighting?
I'm specifying Ruby specifically because I'd like to find some plugins for it. Note that I am not using Rails, and answers should be rails-independent of possible. But I would appreciate some general vim-ness koans about programming in other languages, as well.
Thank you, and I hope my question isn't too vague or inspires any nasty editor wars.
I use vim for all my Ruby programming, and I think its customizability is its killer feature. With vim you can do just about everything to text you can imagine, if you're willing to invest the time to hunt down the plugins that do what you want or write a few scripts yourself.
I'll just list a few things I like about vim for programming (in no particular order):
Syntax Highlighting
The ruby syntax highlighting is very nice. One thing in particular that seems to be somewhat unique is that keywords can be colored differently depending on context. This isn't used as much as I would like, but you can easily see whether that end statement closes, say, an if-statement or a function definition.
Also nice is that, since vim knows which parts of the text are comments, you get spell checking for these only.
Automatic indenting.
When you're writing code, vim will automatically place the cursor at the right indentation level, so you don't have to worry about that. I also find myself invoking this functionality manually by selecting a block and pressing = to automatically (re-)indent everything I highlighted.
Autocompletion
I use a plugin that automatically pops up completions.These are very versatile. They know the methods of classes from the standard library, look at other files you have open (good for variable names and class methods), recognize when you're typing a filename, etc.
Snippets
There is a plugin called snipMate that provides shortcuts for often used text snippets. It's a big help with writing tests and the like.
Code folding
Scriptability
As I said, vim's scripting is very powerful. Want extraneous whitespace at the end of the line deleted automatically? Just write a one-line script.
Plugins, Plugins, Plugins!
There are a ton of plugins that help you with all kinds of things. Git integration, Rails integration, Rspec integration, autoclosing parentheses, matching keywords that open a block {def,do,if,while, etc.} to their end... the list is practically endless.
It's an exceedingly powerful editor out of the box, it integrates well with version control, and there are bucketloads of good add-ons available. (See the scripts page as well as the tips wiki.) Those are good reasons to consider Vim, but there are plenty of other good editors available for various platforms. (Look, Ma, no religious editor wars!)
In terms of very Ruby-specific add-ons, check out endwise by Tim Pope, as an example. (It automatically inserts end after do, if, etc.) Actually nearly all of Tim Pope's scripts are potentially useful for Rubyists.
How do I get syntax highlighting?
You need at least a minimal .vimrc or .gvimrc to get syntax highlighting and automatic indentation (assuming you want that). Vim ships with examples that can get you started, and if you search for 'vimrc' or 'gvimrc', you will get plenty of hits. That said, here's some of mine to get you started:
" Most general settings first
set nocompatible " Set Vim rather than Vi settings; must go first
set noeb " Set no audio or visual error beep
set bs=indent,eol,start " Backspace over everything in insert mode
set history=500 " Keep 50 lines of command line history
" Set items for view # bottom of windows
set ruler " Show the cursor position all the time
set showcmd " Display incomplete commands
set showmode " Display current mode
set ls=2 " Always show status bar
" Syntax basics
syntax on
filetype indent on
set autoindent
set smartindent
filetype plugin on
" Text basics
set textwidth=80 " Set text to wrap at 80 columns
set expandtab " Convert tabs to spaces
set tabstop=4 " Tabs = 4 spaces
set shiftwidth=4 " Indent/outdent 4 spaces
set softtabstop=4 " Tab key indents
set shiftround " Indent/outdent to nearest tabstop
set smarttab " Uses shiftwidth # start of lines
set fo=trcn
" An exception for Ruby files
autocmd FileType ruby set tabstop=2
autocmd FileType ruby set shiftwidth=2
autocmd FileType ruby set softtabstop=2
autocmd FileType ruby set number
" Search basics
set incsearch " Do incremental searching
set showmatch " Show matching brackets
set hlsearch " Highlight all matches in a search
" Don't use Ex mode, use Q for formatting
map Q gq
" Pick a colorscheme
colorscheme Dim
My personal killer feature of vim is the humble . command. This command repeats the last edit at the current cursor position. This can save oodles of time.
For me, one of the "killer features" of vim is it's ad-hoc macros.
Press q then a key name to store the macro in (I often use m for a macro mnemonic, but any letter is fine) and you'll notice the recording status at the bottom. Now, any key strokes you press will be recorded until you press q again, and you've recorded a macro in the letter m. Now type #m and your keystrokes will be played back, with all their implications, starting from the current cursor position. Press 20#m and you'll replay the macro 20 times, and now you've got a powerful tool for programmatically editing text without the overhead of writing a larger program (or configuration file).
For Ruby specifically, the syntax highlighting (:syn on), automatic indent (:set cindent), and paren/bracket paring (% to move the cursor to the matching brace) and other features can be found in other editors, as you mention. But really, the general text processing macros in vim are a big advantage for any text file.
I think the main advantage VIM has is the fact it's cross platform. Now you're using notepad++, that is a great editor(even if it hasn't good macros capabilities). Tomorrow, you could be obliged to use another OS, and you should learn to use another text editor.
I know where you are coming from. I used IDEs and even notepad++ for longer than I care to remember. If all you use VIM for is basic navigation, you aren't going to be using it to its full potential. while there are several very powerful plugins, I don't believe that finding the right plugin is what makes VIM so powerful.
What does it for me is that my hands do not move from the keyboard and it allows me to stay in the zone. Every time I go to the mouse, it invariably leads to a concentration break - which as a programmer is the unforgivable sin.
The other killer feature for me is searching and replacing using regex. I highly recommend learning more about it. Or in the words of Jamis Buck, "Know Thy Tools".
ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
is a great way to get started, but I think I would recommend the O-Reilly book over this one if you want to spend the money. It is easy to get the basics in VIM, but mastering it takes time - but they payoff is immense.

Capturing keycodes in shell?

I am writing my own shell and need to implement a history feature where up and down arrow keys show history of commands executed. I need to find out when up and down keys are pressed.
How do i do this?
you want to be capturing input in raw mode. this can get kinda complicated, but here's an example that should get you on the right path:
http://docs.linux.cz/programming/c/unix_examples/raw.html
i'm assuming you're writing your shell in c. if you're using a more high-level language, there might be an easy way to get raw input. in python, for instance, i would use the ncurses module.

Resources