Guest User

transparent background

a guest
Nov 10th, 2020
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. // This widget was created by Max Zeryck @mzeryck
  2.  
  3. /*
  4. You can't add commit messages to gists, so I just want to say thanks to everyone who has used, modified,
  5. and enjoyed this script. This version adds support for the iPhone 12 and 12 Pro, thanks to tslagle13!
  6. */
  7.  
  8. // Widgets are unique based on the name of the script.
  9. const filename = Script.name() + ".jpg"
  10. const files = FileManager.local()
  11. const path = files.joinPath(files.documentsDirectory(), filename)
  12.  
  13. if (config.runsInWidget) {
  14. let widget = new ListWidget()
  15. widget.backgroundImage = files.readImage(path)
  16.  
  17. // You can your own code here to add additional items to the "invisible" background of the widget.
  18.  
  19. Script.setWidget(widget)
  20. Script.complete()
  21.  
  22. /*
  23. * The code below this comment is used to set up the invisible widget.
  24. * ===================================================================
  25. */
  26. } else {
  27.  
  28. // Determine if user has taken the screenshot.
  29. var message
  30. message = "Before you start, go to your home screen and enter wiggle mode. Scroll to the empty page on the far right and take a screenshot."
  31. let exitOptions = ["Continue","Exit to Take Screenshot"]
  32. let shouldExit = await generateAlert(message,exitOptions)
  33. if (shouldExit) return
  34.  
  35. // Get screenshot and determine phone size.
  36. let img = await Photos.fromLibrary()
  37. let height = img.size.height
  38. let phone = phoneSizes()[height]
  39. if (!phone) {
  40. message = "It looks like you selected an image that isn't an iPhone screenshot, or your iPhone is not supported. Try again with a different image."
  41. await generateAlert(message,["OK"])
  42. return
  43. }
  44.  
  45. // Prompt for widget size and position.
  46. message = "What size of widget are you creating?"
  47. let sizes = ["Small","Medium","Large"]
  48. let size = await generateAlert(message,sizes)
  49. let widgetSize = sizes[size]
  50.  
  51. message = "What position will it be in?"
  52. message += (height == 1136 ? " (Note that your device only supports two rows of widgets, so the middle and bottom options are the same.)" : "")
  53.  
  54. // Determine image crop based on phone size.
  55. let crop = { w: "", h: "", x: "", y: "" }
  56. if (widgetSize == "Small") {
  57. crop.w = phone.small
  58. crop.h = phone.small
  59. let positions = ["Top left","Top right","Middle left","Middle right","Bottom left","Bottom right"]
  60. let position = await generateAlert(message,positions)
  61.  
  62. // Convert the two words into two keys for the phone size dictionary.
  63. let keys = positions[position].toLowerCase().split(' ')
  64. crop.y = phone[keys[0]]
  65. crop.x = phone[keys[1]]
  66.  
  67. } else if (widgetSize == "Medium") {
  68. crop.w = phone.medium
  69. crop.h = phone.small
  70.  
  71. // Medium and large widgets have a fixed x-value.
  72. crop.x = phone.left
  73. let positions = ["Top","Middle","Bottom"]
  74. let position = await generateAlert(message,positions)
  75. let key = positions[position].toLowerCase()
  76. crop.y = phone[key]
  77.  
  78. } else if(widgetSize == "Large") {
  79. crop.w = phone.medium
  80. crop.h = phone.large
  81. crop.x = phone.left
  82. let positions = ["Top","Bottom"]
  83. let position = await generateAlert(message,positions)
  84.  
  85. // Large widgets at the bottom have the "middle" y-value.
  86. crop.y = position ? phone.middle : phone.top
  87. }
  88.  
  89. // Crop image and finalize the widget.
  90. let imgCrop = cropImage(img, new Rect(crop.x,crop.y,crop.w,crop.h))
  91.  
  92. message = "Your widget background is ready. Would you like to use it as this script's background, or export the image for use in a different script or another widget app?"
  93. const exportPhotoOptions = ["Use for this script","Export to Photos","Export to Files"]
  94. const exportPhoto = await generateAlert(message,exportPhotoOptions)
  95.  
  96. if (exportPhoto == 0) {
  97. files.writeImage(path,imgCrop)
  98. } else if (exportPhoto == 1) {
  99. Photos.save(imgCrop)
  100. } else if (exportPhoto == 2) {
  101. await DocumentPicker.exportImage(imgCrop)
  102. }
  103.  
  104. Script.complete()
  105. }
  106.  
  107. // Generate an alert with the provided array of options.
  108. async function generateAlert(message,options) {
  109.  
  110. let alert = new Alert()
  111. alert.message = message
  112.  
  113. for (const option of options) {
  114. alert.addAction(option)
  115. }
  116.  
  117. let response = await alert.presentAlert()
  118. return response
  119. }
  120.  
  121. // Crop an image into the specified rect.
  122. function cropImage(img,rect) {
  123.  
  124. let draw = new DrawContext()
  125. draw.size = new Size(rect.width, rect.height)
  126.  
  127. draw.drawImageAtPoint(img,new Point(-rect.x, -rect.y))
  128. return draw.getImage()
  129. }
  130.  
  131. // Pixel sizes and positions for widgets on all supported phones.
  132. function phoneSizes() {
  133. let phones = {
  134.  
  135. // 12 and 12 Pro
  136. "2532": {
  137. small: 474,
  138. medium: 1014,
  139. large: 1062,
  140. left: 78,
  141. right: 618,
  142. top: 231,
  143. middle: 819,
  144. bottom: 1407
  145. },
  146.  
  147. // 11 Pro Max, XS Max
  148. "2688": {
  149. small: 507,
  150. medium: 1080,
  151. large: 1137,
  152. left: 81,
  153. right: 654,
  154. top: 228,
  155. middle: 858,
  156. bottom: 1488
  157. },
  158.  
  159. // 11, XR
  160. "1792": {
  161. small: 338,
  162. medium: 720,
  163. large: 758,
  164. left: 54,
  165. right: 436,
  166. top: 160,
  167. middle: 580,
  168. bottom: 1000
  169. },
  170.  
  171.  
  172. // 11 Pro, XS, X
  173. "2436": {
  174. small: 465,
  175. medium: 987,
  176. large: 1035,
  177. left: 69,
  178. right: 591,
  179. top: 213,
  180. middle: 783,
  181. bottom: 1353
  182. },
  183.  
  184. // Plus phones
  185. "2208": {
  186. small: 471,
  187. medium: 1044,
  188. large: 1071,
  189. left: 99,
  190. right: 672,
  191. top: 114,
  192. middle: 696,
  193. bottom: 1278
  194. },
  195.  
  196. // SE2 and 6/6S/7/8
  197. "1334": {
  198. small: 296,
  199. medium: 642,
  200. large: 648,
  201. left: 54,
  202. right: 400,
  203. top: 60,
  204. middle: 412,
  205. bottom: 764
  206. },
  207.  
  208.  
  209. // SE1
  210. "1136": {
  211. small: 282,
  212. medium: 584,
  213. large: 622,
  214. left: 30,
  215. right: 332,
  216. top: 59,
  217. middle: 399,
  218. bottom: 399
  219. },
  220.  
  221. // 11 and XR in Display Zoom mode
  222. "1624": {
  223. small: 310,
  224. medium: 658,
  225. large: 690,
  226. left: 46,
  227. right: 394,
  228. top: 142,
  229. middle: 522,
  230. bottom: 902
  231. },
  232.  
  233. // Plus in Display Zoom mode
  234. "2001" : {
  235. small: 444,
  236. medium: 963,
  237. large: 972,
  238. left: 81,
  239. right: 600,
  240. top: 90,
  241. middle: 618,
  242. bottom: 1146
  243. },
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment