Programming is an interesting profession. You fail dozens or hundreds of times a day, then take a moment to celebrate a little victory. Then you move on to your next failure/success.
Earlier today I needed a function that would return a desired number of blank spaces so I could “pad” some output as desired. For some reason my mind went blank and I forgot about using a printf-style solution, and it quickly went to, “How can I write a Scala function to return
n number of blank spaces?”
Note: If you just want the “best” solution, please scroll to the bottom of this article.
for loop FAQ: How do I use an AppleScript
for loop? (Also, how do you use an AppleScript while loop?)
This is actually a bit of a trick question, as there is no AppleScript
for loop or
while loop syntax. Instead you use the AppleScript
repeat command, as shown in the following examples.
AppleScript for loop examples
Where you might expect an AppleScript for loop to iterate over a list, you use the AppleScript
repeat with syntax:
AppleScript list FAQ: How do I iterate/loop over an AppleScript list?
As I continue to work on my Mac speech recognition software projects, I've often found the need to loop over an AppleScript list. One way to loop over an AppleScript list is to use the AppleScript repeat with syntax:
AppleScript “list” FAQ: Can you share some simple AppleScript list examples?
Sure. As I've been doing a lot of work with Mac speech recognition software lately I've been working a lot of with AppleScript lists. Let's take a look at some common AppleScript list examples (and programming needs).
How to create an AppleScript list
A simple AppleScript list can be created like this:
AppleScript “list” FAQ: How do I do something for every item in an AppleScript list? (or, How do I loop over an AppleScript list?)
Loop over an AppleScript list with the every keyword
I've shown examples of the AppleScript every keyword in other posts, so rather than create anything new, here's a repeat of one of those examples:
while loop FAQ: Can you share some examples of the AppleScript
while loop syntax (the AppleScript repeat while syntax)?
Instead of using a keyword like "while" to create loops, AppleScript uses the "repeat" keyword to create loops. There are several versions of this syntax, as follows.
AppleScript repeat syntax: Repeat X times
Here's how to repeat an AppleScript command X number of times:
Another cool feature of the vi and vim editors is that you can easily re-use some search and replace commands across files. For example, I was just editing several HTML files and needed to do the exact same thing to each file: find a certain line, and then delete that line and the line right after it. Fortunately this is very easy to do in vi.
To edit multiple files in vi I first issue this command from the Linux command prompt: