Skip to content

Instantly share code, notes, and snippets.

-- using list in AppleScript
-- define a list
set emptyList to {}
set d to {"c", "d"}
set e to {"e", "f"}
set ne to {"a", "b", 1, 3, d, e}
-- count items in a list
count ne
--> 6
@thomasjao
thomasjao / gist:7853169
Created December 8, 2013 03:53
Using UI elements to access unsupported application "Dictionary"
(* Appliction "Dictionary" doesn't support AppleScript. However, we can control this application via UI elements
of System Events. Following example show how to access various parts of "Dictionary" *)
tell application "Dictionary"
activate
tell application "System Events" to tell process "Dictionary"
UI elements of window 1
(* Access Spot light, enter searching word in the spotlight *)
set value of text field 1 of group 3 of tool bar 1 of windows to "challenge"
(* Get matched words/phrases list on the side bar of "Dictionary" *)
value of text field 1 of row 2 of table 1 of scroll area 1 of splitter group 1 of window 1
@thomasjao
thomasjao / stripComma
Last active August 29, 2015 13:56
strip commas off from formatted digits to real number or integer
// NOTE: ensure numString contains only validate value, e.g. only digits, comma and period allowed
function toReal( numString ) {
if ( numString.contains('.') ) { // deem as real number with period within, regardless if comma exists
return parseFloat(numString.replace(/,/g, ''));
} else {
return parseInt( numSring ); // deem as integer, without period
}
}
// SIMPLE VARIABLE ASSIGNMENT
// Using syntax: set ... to ...
set a to 1 -- set a as an integer object with value 1
set PI to 3.14159 -- set PI as a real object with value 3.14159
set b to "A string" -- set b as a text object with value "A string"
set c to {"Thomas", "Sally"} -- set c as a list object contains two items: "Thomas", "Sally"
set d to {name: "Thomas", sex: "male"} -- set d as a record object with two properties with values
// To get variable type, in AppleScript, we name variable type as "class"
class of a -- return "integer"
@thomasjao
thomasjao / gist:9602291
Created March 17, 2014 16:09
HTML -- Hide partial of internal DOM object within an outer DOM object
<!-- This snippet demo how to HIDE partial of a division within some range (another division)
If replace #d2 below with images can make parts of the image shown within #s1 range.
Also some suggest to add "margin: 0 auto;" within #s1 CSS, don't know why?
-->
<!DOCTYPE html>
<head>
<style>
#s1, #s2 { width: 300x; height: 200px; }
#s1 { top: 50px; background-color: blue; overflow: hidden; } /* assume #s1 is the range */
#s2 { background-color: brown; left: 100px; position: relative; }
@thomasjao
thomasjao / store date as JSON
Created September 9, 2014 00:03
JSON 的資料型態不包含 date,往往需另行處理日期相關的運算,頗為麻煩。若能將日期儲存為數值,則於運算時可便利很多。
var birthday = '1962/7/25';
function date2JSON( dt ) {
var dayArr = dt.split('/');
return JSON.parse( '{ "year":' + parseInt( dt[0] ) + ', "month:"' + parseInt( dt[1] ) + ', "day":' + parseInt( dt[3] ) + '}');
Usage Example:
var myBirthday = date2JSON( birthday );
==> {"year": 1962, "month": 7, "day": 25}
@thomasjao
thomasjao / README.md
Last active August 29, 2015 14:08
漢語拼音

##漢語拼音 漢語拼音的好處:

  • 祇要熟悉英文字母字鍵,不用額外學習音碼或型碼的按鍵位置
  • 以「辭」為單位輸入,大幅減少選字時間
  • 成語、常見辭句可直接輸入個別字元的首碼,大幅減少鍵入時間
  • 不經切換鍵即可直接輸入標點符號
@thomasjao
thomasjao / PHP data type conversion
Created January 27, 2015 06:23
Convert date string into MySQL Date format
$origin = '97.09.27';
$dt = preg_split('/\./', $origin);
$mysqlDate = ((int)$dt[0] + 1911).'-'.$dt[1].'-'.dt[2];
1. preg_split has at least 2 parameters:
1) '/\./' -- regular expression pattern
2) $origin -- the string to be splited
it use regular expression pattern to separate the string into array
2. convert string to number (coersion)
@thomasjao
thomasjao / epoch of Taiwan to AD
Created January 29, 2015 14:36
Convert epoch of Taiwan to AD
/* Epoch used in Taiwan started since 1911 AD. For example, this year (2015 AD) in Taiwan epoch is 中華民國 104 年
Often we need to different epoch notation between system/languages */
/* From String to JavaScript
Taiwan epoch notation -- '104.01.29'
AD -- Thu Jan 29 2015 22:27:19 GMT+0800 (CST) */
var str2JSdate = function( str ) {
var dt = new Date();
var date_arr = str.split('.');
dt.setFullYear( parseInt( date_arr[0] ) + 1911 );
@thomasjao
thomasjao / gist:2d21a0059c626f920f35
Created January 29, 2015 16:02
Dynamically Add Script Using JavaScript Code
var script = document.createElement('script');
script.setAttribute('src', 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js');
document.getElementsByTagName('head')[0].appendChild( script );