Fixed getScreenWithBounds

Fixed getScreenWithBounds

global _cache
set _cache to {}

on split(_str, _delimiter)
	set _oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to _delimiter
	set _arr to every text item of _str
	set AppleScript's text item delimiters to _oldDelimiters
	return _arr
end split

on getAllScreens()
	try
		set _tmp to getAllScreens of _cache
		return _tmp
	end try

	tell application "System Events"
		tell dock preferences
			set _autohide to autohide
		end tell
	end tell

	set _aspath to ((path to me as text) & "::getScreenInfo")
	set _utilpath to POSIX path of _aspath

	set _lines to paragraphs of (do shell script _utilpath)
	set _dockHeight to word 1 of (item 2 of _lines) as number
	set _dockScreen to word 2 of (item 2 of _lines) as number

	set _screens to {}

	set _linesOffset to 3
	repeat (item 1 of _lines as number) times
		set _screenIndex to _linesOffset - 2
		set _screenInfo to split(item _linesOffset of _lines, " ")
		set _originX to item 1 of _screenInfo as integer
		set _originY to item 2 of _screenInfo as integer
		set _width to item 3 of _screenInfo as integer
		set _height to item 4 of _screenInfo as integer
		(* HANDLE DOCK *)
		(* TODO: RETHINK *)
		if _dockScreen = _screenIndex then
			set _height to _height - _dockHeight
		end if
		(* END HANDLING DOCK *)
		set _screens to _screens & {{screenIndex:_screenIndex, originX:_originX, originY:_originY, width:_width, height:_height}}
		set _linesOffset to _linesOffset + 1
	end repeat

	set _result to {dock:{autohide:_autohide, height:_dockHeight, screenIndex:_dockScreen}, screens:_screens}
	set _cache to _cache & {getAllScreens:_result}

	return _result
end getAllScreens

on getScreenWithCoordinates(_x, _y)
	set _screenInfo to getAllScreens()
	set _screens to screens of _screenInfo
	set _index to 1
	repeat (count of _screens) times
		set _screen to item _index of _screens
		if (originX of _screen) <= _x and _x < (originX of _screen) + (width of _screen) then
			if (originY of _screen) <= _y and _y < (originY of _screen) + (height of _screen) then
				return _screen
			end if
		end if
		set _index to _index + 1
	end repeat
	return {}
end getScreenWithCoordinates

on getScreenWithBounds(_bounds)
	set _lx to item 1 of _bounds
	set _ly to item 2 of _bounds
	set _rx to item 3 of _bounds
	set _ry to item 4 of _bounds
	set _result to getScreenWithCoordinates(_lx, _ly)
	if _result = {} then
		set _result to getScreenWithCoordinates(_rx, _ry)
	end if

	if _result = {} then
		-- silence warning if fullscreen
		if not _ly < 20 then
			set _alertResponse to display alert "Something wierd happened. " message "Your window appears to be entirely off screen. Please post an issue on GitHub attaching a screenshot and a brief description." as critical buttons {"Go to GitHub", "Never mind"} default button "Go to GitHub"
			if button returned of _alertResponse = "Go to Github" then
				tell application "Safari" to open location "https://github.com/apaszke/termtile/issues"
			end if
		end if
		error "getScreenWithBounds: No screen found for bounds {" & _lx & ", " & _ly & ", " & _rx & ", " & _ry & "}" number 501
	end if

	return _result
end getScreenWithBounds

on getScreenWithFrontmostWindowOfApp(_appName)
	using terms from application "Terminal"
		tell application _appName
			set _bounds to bounds of window 0
		end tell
	end using terms from

	return getScreenWithBounds(_bounds)
end getScreenWithFrontmostWindowOfApp

global _cache
set _cache to {}

on split(_str, _delimiter)
	set _oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to _delimiter
	set _arr to every text item of _str
	set AppleScript's text item delimiters to _oldDelimiters
	return _arr
end split

on getAllScreens()
	try
		set _tmp to getAllScreens of _cache
		return _tmp
	end try

	tell application "System Events"
		tell dock preferences
			set _autohide to autohide
		end tell
	end tell

	set _aspath to ((path to me as text) & "::getScreenInfo")
	set _utilpath to POSIX path of _aspath

	set _lines to paragraphs of (do shell script _utilpath)
	set _dockHeight to word 1 of (item 2 of _lines) as number
	set _dockScreen to word 2 of (item 2 of _lines) as number

	set _screens to {}

	set _linesOffset to 3
	repeat (item 1 of _lines as number) times
		set _screenIndex to _linesOffset - 2
		set _screenInfo to split(item _linesOffset of _lines, " ")
		set _originX to item 1 of _screenInfo as integer
		set _originY to item 2 of _screenInfo as integer
		set _width to item 3 of _screenInfo as integer
		set _height to item 4 of _screenInfo as integer
		(* HANDLE DOCK *)
		(* TODO: RETHINK *)
		if _dockScreen = _screenIndex then
			set _height to _height - _dockHeight
		end if
		(* END HANDLING DOCK *)
		set _screens to _screens & {{screenIndex:_screenIndex, originX:_originX, originY:_originY, width:_width, height:_height}}
		set _linesOffset to _linesOffset + 1
	end repeat

	set _result to {dock:{autohide:_autohide, height:_dockHeight, screenIndex:_dockScreen}, screens:_screens}
	set _cache to _cache & {getAllScreens:_result}

	return _result
end getAllScreens

on getScreenWithCoordinates(_x, _y)
	set _screenInfo to getAllScreens()
	set _screens to screens of _screenInfo
	set _index to 1
	repeat (count of _screens) times
		set _screen to item _index of _screens
		if (originX of _screen) <= _x and _x < (originX of _screen) + (width of _screen) then
			if (originY of _screen) <= _y and _y < (originY of _screen) + (height of _screen) then
				return _screen
			end if
		end if
		set _index to _index + 1
	end repeat
	return {}
end getScreenWithCoordinates

on getScreenWithBounds(_bounds)
	set _lx to item 1 of _bounds
	set _ly to item 2 of _bounds
	set _rx to item 3 of _bounds
	set _ry to item 4 of _bounds
	set _result to getScreenWithCoordinates(_lx, _ly)
	if _result = {} then
		set _result to getScreenWithCoordinates(_rx, _ry)
	end if

	-- silence warning if fullscreen
	if _result = {} and not _ly < 20 then
		set _alertResponse to display alert "Something wierd happened. " message "Your window appears to be entirely off screen. Please post an issue on GitHub attaching a screenshot and a brief description." as critical buttons {"Go to GitHub", "Never mind"} default button "Go to GitHub"
		if button returned of _alertResponse = "Go to Github" then
			tell application "Safari" to open location "https://github.com/apaszke/termtile/issues"
		end if
	end if
	error "getScreenWithBounds: No screen found for bounds {" & _lx & ", " & _ly & ", " & _rx & ", " & _ry & "}" number 501
end getScreenWithBounds

on getScreenWithFrontmostWindowOfApp(_appName)
	using terms from application "Terminal"
		tell application _appName
			set _bounds to bounds of window 0
		end tell
	end using terms from

	return getScreenWithBounds(_bounds)
end getScreenWithFrontmostWindowOfApp


should eliminate duplicates now if no new things to add

on split(theString, theDelimiter)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theArray to every text item of theString
	set AppleScript's text item delimiters to oldDelimiters
	return theArray
end split

on join(theList, theDelimiter)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set output to "" & theList
	set AppleScript's text item delimiters to prevTIDs
	return output
end join

on run argv
	set CALLDB to item 1 of argv
	set CONTACTDB to item 2 of argv
	set PHONENAME to item 3 of argv
	set CALID to item 4 of argv
	set LASTID to item 5 of argv
	--set CALLDB to "/Users/ryanlee/Library/Application Support/MobileSync/Backup/185128b0d50c97e1e386e6a82ac33605d3589e2a/2b2b0084a1bc3a5ac8c27afdf14afb42c61a19ca"
	--set CONTACTDB to "/Users/ryanlee/Library/Application Support/MobileSync/Backup/185128b0d50c97e1e386e6a82ac33605d3589e2a/31bb7ba8914766d4ba40d6dfb6113c8b614be442"
	--set PHONENAME to "Moore"
	--set CALID to "3C67DB8B-2A06-4A04-BBB5-9D7C07986712"
	--set LASTID to 1580
	set RET to LASTID
	set tzOffset to time to GMT
	set main_query to "attach '" & CONTACTDB & "' as ab; SELECT call.rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, abp.first, abp.last, 'outgoing' from call, ab.ABPerson as abp where call.rowid > " & LASTID & " and flags&1=1 and id=abp.rowid union SELECT call.rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, abp.first, abp.last, 'incoming' from call, ab.ABPerson as abp, ab.ABMultiValue as mv where call.rowid > " & LASTID & " and flags&1=0 and address=replace(replace(replace(replace(replace(replace(replace(mv.value,'.',''),'-',''),' ',''),'(',''),')',''),'+1',''),'+','') and mv.record_id=abp.rowid union SELECT call.rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, abp.first, abp.last, 'outgoing' from call, ab.ABPerson as abp, ab.ABMultiValue as mv where id=-1 and call.rowid > " & LASTID & " and flags&1=1 and address=replace(replace(replace(replace(replace(replace(replace(mv.value,'.',''),'-',''),' ',''),'(',''),')',''),'+1',''),'+','') and mv.record_id=abp.rowid;"
	set rows to paragraphs of (do shell script "/usr/bin/sqlite3 -batch \"" & CALLDB & "\" \"" & main_query & "\"")
	set found_ids to {}
	repeat with i from 1 to (length of rows)
		set eventInfo to my split(item i of rows, "|")
		set found_ids to found_ids & item 1 of eventInfo
	end repeat
	set sub_query to "SELECT rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, address, '', 'outgoing' FROM call WHERE call.rowid > " & LASTID & " AND flags&1=1 AND id=-1 AND rowid not in (" & my join(found_ids, ",") & ") UNION SELECT rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, address, '', 'incoming' FROM call WHERE call.rowid > " & LASTID & " AND flags&1=0 AND id=-1 AND rowid not in (" & my join(found_ids, ",") & ")"
	set rows to rows & paragraphs of (do shell script "/usr/bin/sqlite3 -batch \"" & CALLDB & "\" \"" & sub_query & "\"")
	tell application "iCal"
		set theCalendar to first calendar whose uid = CALID
		set now to current date
		tell theCalendar
			repeat with row in rows
				set eventStart to current date
				set eventEnd to current date
				set eventInfo to my split(row, "|")
				set eventSummary to item 7 of eventInfo
				if item 8 of eventInfo is not "" then
					set eventSummary to eventSummary & " " & item 8 of eventInfo
				end if
				set eventSummary to eventSummary & " (" & item 9 of eventInfo & ")"
				set eventURN to "urn:tel:record:" & PHONENAME & ":" & item 1 of eventInfo
				set eventStartParts to my split(item 3 of eventInfo, "-")
				set eventEndParts to my split(item 4 of eventInfo, "-")
				set year of eventStart to item 1 of eventStartParts
				set month of eventStart to item 2 of eventStartParts
				set day of eventStart to item 3 of eventStartParts
				set hours of eventStart to item 4 of eventStartParts
				set minutes of eventStart to item 5 of eventStartParts
				set seconds of eventStart to item 6 of eventStartParts
				set year of eventEnd to item 1 of eventEndParts
				set month of eventEnd to item 2 of eventEndParts
				set day of eventEnd to item 3 of eventEndParts
				set hours of eventEnd to item 4 of eventEndParts
				set minutes of eventEnd to item 5 of eventEndParts
				set seconds of eventEnd to item 6 of eventEndParts
				make new event at end of events with properties {summary:eventSummary, location:PHONENAME, start date:eventStart, end date:eventEnd, allday event:false, stamp date:now, url:eventURN}
				if item 1 of eventInfo as integer > RET then
					set RET to item 1 of eventInfo as integer
				end if
			end repeat
		end tell
	end tell
	return RET
end run

on split(theString, theDelimiter)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theArray to every text item of theString
	set AppleScript's text item delimiters to oldDelimiters
	return theArray
end split

on join(theList, theDelimiter)
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set output to "" & theList
	set AppleScript's text item delimiters to prevTIDs
	return output
end join

on run argv
	set RET to 0
	set CALLDB to item 1 of argv
	set CONTACTDB to item 2 of argv
	set PHONENAME to item 3 of argv
	set CALID to item 4 of argv
	set LASTID to item 5 of argv
	--set CALLDB to "/Users/ryanlee/Library/Application Support/MobileSync/Backup/185128b0d50c97e1e386e6a82ac33605d3589e2a/2b2b0084a1bc3a5ac8c27afdf14afb42c61a19ca"
	--set CONTACTDB to "/Users/ryanlee/Library/Application Support/MobileSync/Backup/185128b0d50c97e1e386e6a82ac33605d3589e2a/31bb7ba8914766d4ba40d6dfb6113c8b614be442"
	--set PHONENAME to "Moore"
	--set CALID to "3C67DB8B-2A06-4A04-BBB5-9D7C07986712"
	--set LASTID to 0
	set tzOffset to time to GMT
	set main_query to "attach '" & CONTACTDB & "' as ab; SELECT call.rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, abp.first, abp.last, 'outgoing' from call, ab.ABPerson as abp where call.rowid > " & LASTID & " and flags&1=1 and id=abp.rowid union SELECT call.rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, abp.first, abp.last, 'incoming' from call, ab.ABPerson as abp, ab.ABMultiValue as mv where call.rowid > " & LASTID & " and flags&1=0 and address=replace(replace(replace(replace(replace(replace(replace(mv.value,'.',''),'-',''),' ',''),'(',''),')',''),'+1',''),'+','') and mv.record_id=abp.rowid union SELECT call.rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, abp.first, abp.last, 'outgoing' from call, ab.ABPerson as abp, ab.ABMultiValue as mv where id=-1 and call.rowid > " & LASTID & " and flags&1=1 and address=replace(replace(replace(replace(replace(replace(replace(mv.value,'.',''),'-',''),' ',''),'(',''),')',''),'+1',''),'+','') and mv.record_id=abp.rowid;"
	set rows to paragraphs of (do shell script "/usr/bin/sqlite3 -batch \"" & CALLDB & "\" \"" & main_query & "\"")
	set found_ids to {}
	repeat with i from 1 to (length of rows)
		set eventInfo to my split(item i of rows, "|")
		set found_ids to found_ids & item 1 of eventInfo
	end repeat
	set sub_query to "SELECT rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, address, '', 'outgoing' FROM call WHERE call.rowid > " & LASTID & " AND flags&1=1 AND id=-1 AND rowid not in (" & my join(found_ids, ",") & ") UNION SELECT rowid, address, strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & ",'unixepoch'), strftime('%Y-%m-%d-%H-%M-%S',date" & tzOffset & "+duration,'unixepoch'), flags, id, address, '', 'incoming' FROM call WHERE call.rowid > " & LASTID & " AND flags&1=0 AND id=-1 AND rowid not in (" & my join(found_ids, ",") & ")"
	set rows to rows & paragraphs of (do shell script "/usr/bin/sqlite3 -batch \"" & CALLDB & "\" \"" & sub_query & "\"")
	tell application "iCal"
		set theCalendar to first calendar whose uid = CALID
		set now to current date
		tell theCalendar
			repeat with row in rows
				set eventStart to current date
				set eventEnd to current date
				set eventInfo to my split(row, "|")
				set eventSummary to item 7 of eventInfo
				if item 8 of eventInfo is not "" then
					set eventSummary to eventSummary & " " & item 8 of eventInfo
				end if
				set eventSummary to eventSummary & " (" & item 9 of eventInfo & ")"
				set eventURN to "urn:tel:record:" & PHONENAME & ":" & item 1 of eventInfo
				set eventStartParts to my split(item 3 of eventInfo, "-")
				set eventEndParts to my split(item 4 of eventInfo, "-")
				log eventStartParts
				log eventEndParts
				set year of eventStart to item 1 of eventStartParts
				set month of eventStart to item 2 of eventStartParts
				set day of eventStart to item 3 of eventStartParts
				set hours of eventStart to item 4 of eventStartParts
				set minutes of eventStart to item 5 of eventStartParts
				set seconds of eventStart to item 6 of eventStartParts
				set year of eventEnd to item 1 of eventEndParts
				set month of eventEnd to item 2 of eventEndParts
				set day of eventEnd to item 3 of eventEndParts
				set hours of eventEnd to item 4 of eventEndParts
				set minutes of eventEnd to item 5 of eventEndParts
				set seconds of eventEnd to item 6 of eventEndParts
				make new event at end of events with properties {summary:eventSummary, location:PHONENAME, start date:eventStart, end date:eventEnd, allday event:false, stamp date:now, url:eventURN}
				if item 1 of eventInfo as integer > RET then
					set RET to item 1 of eventInfo as integer
				end if
			end repeat
		end tell
	end tell
	return RET
end run


do pause/refresh after go to Record

do pause/refresh after go to Record

Scripts/fmClip - Loop Script Steps.applescript


Part 2 of the fix for #114 (GrowlTunes hang on dead pictd): Only accept data from artworks that are not QuickDraw pictures.

FasdUAS 1.101.10
k
lr
Qr

Oe
kd

r

mfsck
o0returnvaluereturnValue
r
n
1

pnam
1

pPla
o0alibraryaLibrary
r
1

pTrk
o0atrackaTrack
r !
 m

null
!o0
atrackartwork
aTrackArtwork"#
"r"$%
$l &
&n '(
'2

cArt
(o0atrackaTrack
%o0allartworksallArtworks#)*
)Z#]+,
+@#*-.
-l#(/
/I#(0
.corecnte********
0o#$0allartworksallArtworks
.m()
,Y-Y123
1Z;T45
4>;C67
6n;A89
81?A

pFmt
9n;?:;
:4<?<

cArt
<o=>0i
;o;<0atrackaTrack
7mAB

PICT
5kFP==>?
>rFN@A
@nFLBC
B1JL

pRaw
CnFJDE
D4GJF

cArt
FoHI0i
EoFG0atrackaTrack
Ao0
atrackartwork
aTrackArtwork?G
GSOP0i
2m01
3l16H
HI16I
.corecnte********
Io120allartworksallArtworks*J
Jr^dKL
KJ^bMMNO
No^_0
atrackartwork
aTrackArtworkOP
Po_`0alibraryaLibrary
Lo0returnvaluereturnValue
mQQhookalisLSnow-Leopardƺ8H+
iTunes.appuƀ*	 ApplicationsƻƁM$Snow-Leopard:Applications:iTunes.app
iTunes.appSnow-LeopardApplications/iTunes.app/
	R
.ascrerr ********

rmrRS
RmmpTTUUfsck
So0returnvaluereturnValueVW
VlsuX
XLsuY
Yost0returnvaluereturnValueWZ
Zl[\[
.aevtoappnull****\]^_
.aevtoappnull****
]ku``aaV^0i_QT0returnvaluereturnValue

pPla

pnam0alibraryaLibrary

pTrk0atrackaTrack

null0
atrackartwork
aTrackArtwork

cArt0allartworksallArtworks
.corecnte********

pFmt

PICT

pRawvg_EO*,,EO*,EOEO-EOjk1+kjkh/,/,EOYh[OYYhOlvEUWXaEOascrޭ

FasdUAS 1.101.10
k
lH
QH

O=
k<

r

mfsck
o0returnvaluereturnValue
r
n
1

pnam
1

pPla
o0alibraryaLibrary
r
1

pTrk
o0atrackaTrack
r !
 m

null
!o0
atrackartwork
aTrackArtwork"#
"Z5$%
$@&&'
&l$(
(I$)
.corecnte********
)n *+
*m

cArt
+o0atrackaTrack
'm$%
%r)1,-
,n)/./
.1-/

pRaw
/n)-01
04*-2

cArt
2m+,
1o)*0atrackaTrack
-o0
atrackartwork
aTrackArtwork#3
3r6<45
4J6:6678
7o670
atrackartwork
aTrackArtwork89
9o780alibraryaLibrary
5o0returnvaluereturnValue
m::hookalisLSnow-Leopardƺ8H+
iTunes.appuƀ*	 ApplicationsƻƁM$Snow-Leopard:Applications:iTunes.app
iTunes.appSnow-LeopardApplications/iTunes.app/
	R
.ascrerr ********

rEH;<
;mEF==>>fsck
<o0returnvaluereturnValue?@
?lIKA
ALIKB
BoIJ0returnvaluereturnValue@C
ClDED
.aevtoappnull****EFGH
.aevtoappnull****
FkKIIJJ?GH:=0returnvaluereturnValue

pPla

pnam0alibraryaLibrary

pTrk0atrackaTrack

null0
atrackartwork
aTrackArtwork

cArt
.corecnte********

pRawL?7EO*,,EO*,EOEO,jk
k/,EYhOlvEUW
X
EOascrޭ