Applescript: string might have a quote - applescript

I've got an Applescript file that in part of it checks an API for a string. That string may have a quote in it. When it doesn't the script works fine, when it does have a quote the string fails. How can I safeguard the string so it works in either case?
This works:
string = "Yeah working"
This doesn't:
string = "Yeah I'm working"
How can I escape that quote mark when I don't know where it'll be or if it'll be there?

string isn't a legal variable name for starters. Other than that, set myString to quoted form of "Something own's a quote" should do the trick.

Related

Escape double quotes in qsTrId source string

For a project I'm working on, I'm forced to use qsTrid() that need the following syntax to work:
//% "My string"
text: qsTrId('my_string')
Until you need to use double quotes there are no drawbacks in using this for simple strings, still you cannot use arguments normally like you would do for qsTr().
The problem arise when you need something like this:
//% "My "super" string"
text: qsTrId('my_super_string')
lupdate will only see "My " as source string, since the double quotes are used as delimiter.
Up to now the best I could achieve is this:
//% "My ""super"" string"
That will produce "My super string", the string is not broken but is missing the double quotes.
I've tried searching online and in the documentation if there are special rules in this case, but had no luck.
I tried using single quotes but lupdate does not see the string at all in this case.
I cannot use \" because the source string is used in the other language that will not be translated, but if it is the only solution I'll try to propose it.
Anyone know how to correctly escape the double quotes in this situation?

How to replace single quotes with escaped single quotes in ruby

I'm trying to replace single quotes (') with escaped single quotes (\') in a string in ruby 1.9.3 and 1.8.7.
The exact problem string is "Are you sure you want to delete '%#'". This string should become "Are you sure you want to delete \'%#\'"
Using .gsub!(/\'/,"\'") leads to the following string "Are you sure you want to %#'%#".
Any ideas on what's going on?
String#gsub in the form gsub(exp,replacement) has odd quirks affecting the replacement string which sometimes require lots of escaping slashes. Ruby users are frequently directed to use the block form instead:
str.gsub(/'/){ "\\'" }
If you want to do away with escaping altogether, consider using an alternate string literal form:
str.gsub(/'/){ %q(\') }
Once you get used to seeing these types of literals, using them to avoid escape sequences can make your code much more readable.
\' in a substitution replacement string means "The portion of the original string after the match". So str.gsub!(/\'/,"\\'") replaces the ' character with everything after it - which is what you've noticed.
You need to further escape the backslash in the replacement. .gsub(/'/,"\\\\'") works in my irb console:
irb(main):059:0> puts a.gsub(/'/,"\\\\'")
Are you sure you want to delete \'%#\'
You need to escape the backslash. What about this?
"Are you sure you want to delete '%#'".gsub(/(?=')/, "\\")
# => "Are you sure you want to delete \\'%#\\'"
The above should be what you want. Your expected result is wrong. There is no way to literally see a single backslash when it means literally a backslash.

Ignore embedded quotes in ruby

I'm not exactly sure what is happening, but the xml file I am writing to has this:
This "is" now my String
when it should look like this:
This "is" now my String
Here is the code, except I don't have access to the actual string at compile time. Is there a way to tell the assetName variable to treat embedded quotes as quotes? Thanks.
assetName = 'This "is" now my String'
response.search("property[name=next]").first.andand["value"] = assetName
In XML, quotes may never appear in attributes, regardless of the style of the quotes used for the attributes. It is likely that your XML library is escaping these quotes for you.

How can I remove the string "\n" from within a Ruby string?

I have this string:
"some text\nandsomemore"
I need to remove the "\n" from it. I've tried
"some text\nandsomemore".gsub('\n','')
but it doesn't work. How do I do it? Thanks for reading.
You need to use "\n" not '\n' in your gsub. The different quote marks behave differently.
Double quotes " allow character expansion and expression interpolation ie. they let you use escaped control chars like \n to represent their true value, in this case, newline, and allow the use of #{expression} so you can weave variables and, well, pretty much any ruby expression you like into the text.
While on the other hand, single quotes ' treat the string literally, so there's no expansion, replacement, interpolation or what have you.
In this particular case, it's better to use either the .delete or .tr String method to delete the newlines.
See here for more info
If you want or don't mind having all the leading and trailing whitespace from your string removed you can use the strip method.
" hello ".strip #=> "hello"
"\tgoodbye\r\n".strip #=> "goodbye"
as mentioned here.
edit The original title for this question was different. My answer is for the original question.
When you want to remove a string, rather than replace it you can use String#delete (or its mutator equivalent String#delete!), e.g.:
x = "foo\nfoo"
x.delete!("\n")
x now equals "foofoo"
In this specific case String#delete is more readable than gsub since you are not actually replacing the string with anything.
You don't need a regex for this. Use tr:
"some text\nandsomemore".tr("\n","")
use chomp or strip functions from Ruby:
"abcd\n".chomp => "abcd"
"abcd\n".strip => "abcd"

How to escape slash (/) in VBScript?

How do you escape the forward slash character (/) in VBScript? For example, in the following string:
bob = "VU administration/front desk"
You don't escape it: it doesn't mean anything special in php or vbscript, and therefore doesn't need to be escaped. The only character you need to escape in vbscript is the double quote, which escapes itself:
MyString = "He said, ""Here's how you escape a double quote in vbscript. Slash characters -- both forward (/) and back (\) -- don't mean anything, even when used with common control characters like \n or \t."""
Similarly, in php a backslash escapes itself, but a forward slash doesn't need any special handling.
This is so wrong I have to comment on it. The question is about VBScript, not PHP, so who cares what PHP does with a slash? In VB, the forward slash / does have special meaning. The character / represents DOUBLE. If your code editor displays this character as DOUBLE or you get an error about converting to DOUBLE when you don't mean to convert to DOUBLE then you probably need to fix something in your string.
For instance, VB treats the forward slash / in this query string as DOUBLE and the program would fail here because you cant convert select replace(convert(char(10),pih.updateon,111), from text to double.
Dim sQuery As String = "select replace(convert(char(10),pih.updateon,111),"/","-") as stopdate from myTable"
The problem however will not likely be an issue with escaping a slash. Chances are you goofed somewhere else. In this case, if you typed the string correctly, it would look like this:
Dim sQuery As String = "select replace(convert(char(10),pih.updateon,111),'/','-') as stopdate from myTable"
Hope that example helps understand where you might be going wrong if you are having issues with slashes.
Addendum: In VBScript and VB, the chief difference being VB is compiled and VBScript is interpreted, the syntax in this case is handled the same way.
d-bo

Resources