1# @ohos.pasteboard (Pasteboard) 2<!--Kit: Basic Services Kit--> 3<!--Subsystem: MiscServices--> 4<!--Owner: @yangxiaodong41--> 5<!--Designer: @guo867--> 6<!--Tester: @maxiaorong2--> 7<!--Adviser: @HelloCrease--> 8 9This module provides the capabilities of managing the system pasteboard to support the copy and paste functions. You can use the APIs of this module to operate pasteboard content of the plain text, HTML, URI, Want, pixel map, and other types. 10 11> **NOTE** 12> 13> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 14 15## Modules to Import 16 17```ts 18import { pasteboard } from '@kit.BasicServicesKit'; 19``` 20 21## Constants 22 23**Atomic service API**: This API can be used in atomic services since API version 11. 24 25**System capability**: SystemCapability.MiscServices.Pasteboard 26 27| Name| Type| Value | Description | 28| -------- | -------- |--------------|-------------------------------------------------------------------------------------------------------------------------------------------| 29| MAX_RECORD_NUM<sup>7+</sup> | number | - | Maximum number of records in a **PasteData** object. In versions earlier than API version 10, the value is 512, indicating that no more records can be added once the number of records reaches 512.<br>Since API version 10, no limit is placed on the number of records in a **PasteData** object.| 30| MIMETYPE_TEXT_HTML<sup>7+</sup> | string | 'text/html' | MIME type of the HTML content. | 31| MIMETYPE_TEXT_WANT<sup>7+</sup> | string | 'text/want' | MIME type of the Want content. | 32| MIMETYPE_TEXT_PLAIN<sup>7+</sup> | string | 'text/plain' | MIME type of the plain text content. | 33| MIMETYPE_TEXT_URI<sup>7+</sup> | string | 'text/uri' | MIME type of the URI content. | 34| MIMETYPE_PIXELMAP<sup>9+</sup> | string | 'pixelMap' | MIME type of the pixel map. | 35 36## ValueType<sup>9+</sup> 37 38type ValueType = string | image.PixelMap | Want | ArrayBuffer 39 40Enumerates the value types. 41 42**Atomic service API**: This API can be used in atomic services since API version 11. 43 44**System capability**: SystemCapability.MiscServices.Pasteboard 45 46| Type| Description| 47| -------- | -------- | 48| string | The value is of the string type.| 49| image.PixelMap | The value is of the [image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md) type.| 50| Want | The value is of the [Want](../apis-ability-kit/js-apis-app-ability-want.md) type.| 51| ArrayBuffer | The value is of the **ArrayBuffer** type.| 52 53## pasteboard.createData<sup>9+</sup> 54 55createData(mimeType: string, value: ValueType): PasteData 56 57Creates a **PasteData** object of the specified type. 58 59**Atomic service API**: This API can be used in atomic services since API version 11. 60 61**System capability**: SystemCapability.MiscServices.Pasteboard 62 63**Parameters** 64 65| Name| Type| Mandatory| Description | 66| -------- | -------- | -------- |--------------------------------------------------------------------------------------------------------| 67| mimeType | string | Yes| MIME type of custom data. The value can be a predefined MIME type listed in [Constants](#constants), including HTML, WANT, plain text, URI, and pixel map, or a custom MIME type. The value of **mimeType** cannot exceed 1024 bytes.| 68| value | [ValueType](#valuetype9) | Yes| Content of custom data. | 69 70**Return value** 71 72| Type| Description| 73| -------- | -------- | 74| [PasteData](#pastedata) | **PasteData** object.| 75 76**Error codes** 77 78For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 79 80| Error Code ID| Error Message| 81| -------- | -------- | 82| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 83 84**Example 1** 85 86 ```ts 87 let dataXml = new ArrayBuffer(256); 88 let pasteData: pasteboard.PasteData = pasteboard.createData('app/xml', dataXml); 89 ``` 90 91**Example 2** 92 93 ```ts 94 let dataText = 'hello'; 95 let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, dataText); 96 ``` 97 98## pasteboard.createData<sup>14+</sup> 99 100createData(data: Record<string, ValueType>): PasteData 101 102Creates a **PasteData** object that contains multiple types of data. 103 104**System capability**: SystemCapability.MiscServices.Pasteboard 105 106**Parameters** 107 108| Name| Type| Mandatory| Description | 109| -------- |------------------------------------------------| -------- |-----------| 110| data | [Record](../../quick-start/introduction-to-arkts.md#object-literal)<string, [ValueType](#valuetype9)>| Yes| The key of **Record** can be the MIME type corresponding to the pasteboard data, including HTML, WANT, plain text, URI, and PixelMap defined in [Constants](#constants). Alternatively, the key could be a custom MIME type, whose parameter, the length of **mimeType**, cannot exceed 1024 bytes.<br>The value of **Record** is the data corresponding to the MIME type specified in the key.<br>The first MIME type specified by the key-value in **Record** is used as the default MIME type of the first **PasteDataRecord** in the **PasteData** object. Data of non-default types can be read only by using the [getData](#getdata14) API.| 111 112**Return value** 113 114| Type| Description| 115| -------- | -------- | 116| [PasteData](#pastedata) | **PasteData** object.| 117 118**Error codes** 119 120For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 121 122| Error Code ID| Error Message| 123| -------- | -------- | 124| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 125 126**Example 1** 127 128```ts 129let pasteData: pasteboard.PasteData = pasteboard.createData({ 130 'text/plain': 'hello', 131 'app/xml': new ArrayBuffer(256), 132}); 133``` 134 135**Example 2** 136 137```ts 138let record: Record<string, pasteboard.ValueType> = {}; 139record[pasteboard.MIMETYPE_TEXT_PLAIN] = 'hello'; 140record[pasteboard.MIMETYPE_TEXT_URI] = 'dataability:///com.example.myapplication1/user.txt'; 141let pasteData: pasteboard.PasteData = pasteboard.createData(record); 142``` 143 144## pasteboard.createRecord<sup>9+</sup> 145 146createRecord(mimeType: string, value: ValueType): PasteDataRecord 147 148Creates a **PasteDataRecord** object of the specified type. 149 150**Atomic service API**: This API can be used in atomic services since API version 11. 151 152**System capability**: SystemCapability.MiscServices.Pasteboard 153 154**Parameters** 155 156| Name| Type| Mandatory| Description | 157| -------- | -------- | -------- |-------------------| 158| mimeType | string | Yes| MIME type of custom data. The value can be a predefined MIME type listed in [Constants](#constants), including HTML, WANT, plain text, URI, and pixel map, or a custom MIME type. The value of **mimeType** cannot exceed 1024 bytes. | 159| value | [ValueType](#valuetype9) | Yes| Data content of the specified type. | 160 161**Return value** 162 163| Type| Description| 164| -------- | -------- | 165| [PasteDataRecord](#pastedatarecord7) | A new paste data record of a specified type.| 166 167**Error codes** 168 169For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 170 171| Error Code ID| Error Message| 172| -------- | -------- | 173| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 174 175**Example 1** 176 177 ```ts 178let dataXml = new ArrayBuffer(256); 179let pasteDataRecord: pasteboard.PasteDataRecord = pasteboard.createRecord('app/xml', dataXml); 180 ``` 181 182**Example 2** 183 184 ```ts 185let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 186let record: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'file://com.example.myapplication1/data/storage/el2/base/files/file.txt'); 187pasteData.replaceRecord(0, record); 188 ``` 189 190## pasteboard.getSystemPasteboard 191 192getSystemPasteboard(): SystemPasteboard 193 194Obtains this **SystemPasteboard** object. 195 196**Atomic service API**: This API can be used in atomic services since API version 11. 197 198**System capability**: SystemCapability.MiscServices.Pasteboard 199 200**Return value** 201 202| Type| Description| 203| -------- | -------- | 204| [SystemPasteboard](#systempasteboard) | **SystemPasteboard** object.| 205 206**Example** 207 208```ts 209let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 210``` 211 212## ShareOption<sup>9+</sup> 213 214Enumerates the pasteable ranges of pasteboard data. 215 216**Atomic service API**: This API can be used in atomic services since API version 11. 217 218**System capability**: SystemCapability.MiscServices.Pasteboard 219 220| Name | Value | Description | 221| ---------------------------------- | --- | ------------------------------------------------------------------------------------- | 222| INAPP | 0 | Only intra-application pasting is allowed. | 223| LOCALDEVICE | 1 | Paste is allowed in any application.<!--RP1--><!--RP1End--> | 224| CROSSDEVICE<sup>(deprecated)</sup> | 2 | Paste is allowed in any application across devices.<br>This API is deprecated since API version 12 without any alternative API or method. <!--RP2--><!--RP2End-->| 225 226## pasteboard.createHtmlData<sup>(deprecated)</sup> 227 228createHtmlData(htmlText: string): PasteData 229 230Creates a **PasteData** object of the HTML type. 231> **NOTE** 232> 233> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createData](#pasteboardcreatedata9) instead. 234 235**System capability**: SystemCapability.MiscServices.Pasteboard 236 237**Parameters** 238 239| Name| Type| Mandatory| Description| 240| -------- | -------- | -------- | -------- | 241| htmlText | string | Yes| HTML content.| 242 243**Return value** 244 245| Type| Description| 246| -------- | -------- | 247| [PasteData](#pastedata) | **PasteData** object.| 248 249**Example** 250 251```ts 252let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 253let pasteData: pasteboard.PasteData = pasteboard.createHtmlData(html); 254``` 255 256## pasteboard.createWantData<sup>(deprecated)</sup> 257 258createWantData(want: Want): PasteData 259 260Creates a **PasteData** object of the Want type. 261> **NOTE** 262> 263> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createData](#pasteboardcreatedata9) instead. 264 265**System capability**: SystemCapability.MiscServices.Pasteboard 266 267**Parameters** 268 269| Name| Type| Mandatory| Description| 270| -------- | -------- | -------- | -------- | 271| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| Want content.| 272 273**Return value** 274 275| Type| Description| 276| -------- | -------- | 277| [PasteData](#pastedata) | **PasteData** object.| 278 279**Example** 280 281```ts 282import { Want } from '@kit.AbilityKit'; 283 284let object: Want = { 285 bundleName: "com.example.aafwk.test", 286 abilityName: "com.example.aafwk.test.TwoAbility" 287}; 288let pasteData: pasteboard.PasteData = pasteboard.createWantData(object); 289``` 290 291## pasteboard.createPlainTextData<sup>(deprecated)</sup> 292 293createPlainTextData(text: string): PasteData 294 295Creates a **PasteData** object of the plain text type. 296> **NOTE** 297> 298> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [pasteboard.createData](#pasteboardcreatedata9) instead. 299 300**System capability**: SystemCapability.MiscServices.Pasteboard 301 302**Parameters** 303 304| Name| Type| Mandatory| Description| 305| -------- | -------- | -------- | -------- | 306| text | string | Yes| Plain text.| 307 308**Return value** 309 310| Type| Description| 311| -------- | -------- | 312| [PasteData](#pastedata) | **PasteData** object.| 313 314**Example** 315 316```ts 317let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('content'); 318``` 319 320## pasteboard.createUriData<sup>(deprecated)</sup> 321 322createUriData(uri: string): PasteData 323 324Creates a **PasteData** object of the URI type. 325> **NOTE** 326> 327> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createData](#pasteboardcreatedata9) instead. 328 329**System capability**: SystemCapability.MiscServices.Pasteboard 330 331**Parameters** 332 333| Name| Type| Mandatory| Description| 334| -------- | -------- | -------- | -------- | 335| uri | string | Yes| URI content.| 336 337**Return value** 338 339| Type| Description| 340| -------- | -------- | 341| [PasteData](#pastedata) | **PasteData** object.| 342 343**Example** 344 345```ts 346let pasteData: pasteboard.PasteData = pasteboard.createUriData('dataability:///com.example.myapplication1/user.txt'); 347``` 348## pasteboard.createHtmlTextRecord<sup>(deprecated)</sup> 349 350createHtmlTextRecord(htmlText: string): PasteDataRecord 351 352Creates a **PasteDataRecord** object of the HTML text type. 353> **NOTE** 354> 355> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createRecord](#pasteboardcreaterecord9) instead. 356 357**System capability**: SystemCapability.MiscServices.Pasteboard 358 359**Parameters** 360 361| Name| Type| Mandatory| Description| 362| -------- | -------- | -------- | -------- | 363| htmlText | string | Yes| HTML content.| 364 365**Return value** 366 367| Type| Description| 368| -------- | -------- | 369| [PasteDataRecord](#pastedatarecord7) | **PasteDataRecord** object of the HTML text type.| 370 371**Example** 372 373```ts 374let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 375let record: pasteboard.PasteDataRecord = pasteboard.createHtmlTextRecord(html); 376``` 377 378## pasteboard.createWantRecord<sup>(deprecated)</sup> 379 380createWantRecord(want: Want): PasteDataRecord 381 382Creates a **PasteDataRecord** object of the Want type. 383> **NOTE** 384> 385> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createRecord](#pasteboardcreaterecord9) instead. 386 387**System capability**: SystemCapability.MiscServices.Pasteboard 388 389**Parameters** 390 391| Name| Type| Mandatory| Description| 392| -------- | -------- | -------- | -------- | 393| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| Want content.| 394 395**Return value** 396 397| Type| Description| 398| -------- | -------- | 399| [PasteDataRecord](#pastedatarecord7) | New **PasteDataRecord** object of the Want type.| 400 401**Example** 402 403```ts 404import { Want } from '@kit.AbilityKit'; 405 406let object: Want = { 407 bundleName: "com.example.aafwk.test", 408 abilityName: "com.example.aafwk.test.TwoAbility" 409}; 410let record: pasteboard.PasteDataRecord = pasteboard.createWantRecord(object); 411``` 412 413## pasteboard.createPlainTextRecord<sup>(deprecated)</sup> 414 415createPlainTextRecord(text: string): PasteDataRecord 416 417Creates a **PasteDataRecord** object of the plain text type. 418> **NOTE** 419> 420> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createRecord](#pasteboardcreaterecord9) instead. 421 422**System capability**: SystemCapability.MiscServices.Pasteboard 423 424**Parameters** 425 426| Name| Type| Mandatory| Description| 427| -------- | -------- | -------- | -------- | 428| text | string | Yes| Plain text.| 429 430**Return value** 431 432| Type| Description| 433| -------- | -------- | 434| [PasteDataRecord](#pastedatarecord7) | New **PasteDataRecord** object of the plain text type.| 435 436**Example** 437 438```ts 439let record: pasteboard.PasteDataRecord = pasteboard.createPlainTextRecord('hello'); 440``` 441 442## pasteboard.createUriRecord<sup>(deprecated)</sup> 443 444createUriRecord(uri: string): PasteDataRecord 445 446Creates a **PasteDataRecord** object of the URI type. 447> **NOTE** 448> 449> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.createRecord](#pasteboardcreaterecord9) instead. 450 451**System capability**: SystemCapability.MiscServices.Pasteboard 452 453**Parameters** 454 455| Name| Type| Mandatory| Description| 456| -------- | -------- | -------- | -------- | 457| uri | string | Yes| URI content.| 458 459**Return value** 460 461| Type| Description| 462| -------- | -------- | 463| [PasteDataRecord](#pastedatarecord7) | New **PasteDataRecord** object of the URI type.| 464 465**Example** 466 467```ts 468let record: pasteboard.PasteDataRecord = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt'); 469``` 470 471 472## PasteDataProperty<sup>7+</sup> 473 474Defines the properties of all data records on the pasteboard, including the timestamp, data type, pasteable range, and additional data. The defined properties can be applied to the pasteboard only with the [setProperty](#setproperty9) method. 475 476**Atomic service API**: This API can be used in atomic services since API version 11. 477 478**System capability**: SystemCapability.MiscServices.Pasteboard 479 480| Name| Type| Read-Only| Optional| Description| 481| -------- | -------- | -------- | -------- |-------------------------------| 482| additions<sup>7+</sup> | {[key:string]:object} | No| Yes| Additional property data. It does not allow for dynamic adding of properties. Properties can be added only by re-assigning values. This parameter is left empty by default. For details, see the example of **setProperty**.| 483| mimeTypes<sup>7+</sup> | Array<string> | Yes| No| Non-repeating data types of the data records on the pasteboard.| 484| tag<sup>7+</sup> | string | No| Yes| Custom tag. This parameter is left empty by default.| 485| timestamp<sup>7+</sup> | number | Yes| No| Timestamp when data is written to the pasteboard (unit: ms).| 486| localOnly<sup>7+</sup> | boolean | No| Yes| Whether the pasteboard content is for local access only. The default value is **false**. The value will be overwritten by the value of the **shareOption** attribute. You are advised to use the [ShareOption](#shareoption9) attribute instead.| 487| shareOption<sup>9+</sup> | [ShareOption](#shareoption9) | No| Yes| Pasteable ranges of pasteboard data. The default value is **CROSSDEVICE**.| 488 489## FileConflictOptions<sup>15+</sup> 490 491Defines options for file copy conflicts. 492 493**Atomic service API**: This API can be used in atomic services since API version 15. 494 495**System capability**: SystemCapability.MiscServices.Pasteboard 496 497| Name | Value | Description | 498| --------- | ---- | ------------------------------------------------------------ | 499| OVERWRITE | 0 | Overwrites the file with the same name in the destination path. | 500| SKIP | 1 | Skips the file with the same name in the destination path. If **SKIP** is set, the copied data of the skipped file is not pasted to the application.| 501 502## ProgressIndicator<sup>15+</sup> 503 504Defines options for the progress indicator. You can choose whether to use the default progress indicator. 505 506**Atomic service API**: This API can be used in atomic services since API version 15. 507 508**System capability**: SystemCapability.MiscServices.Pasteboard 509 510| Name | Value | Description | 511| ------- | ---- | ------------------------ | 512| NONE | 0 | The default progress indicator is not used.| 513| DEFAULT | 1 | The default progress indicator is used. | 514 515## ProgressInfo<sup>15+</sup> 516 517Defines the progress information. This information is reported only when [ProgressIndicator](#progressindicator15) is set to **NONE**. 518 519**Atomic service API**: This API can be used in atomic services since API version 15. 520 521**System capability**: SystemCapability.MiscServices.Pasteboard 522 523| Name | Type | Read-Only| Optional| Description | 524| -------- | ------ | ---- | ---- | ---------------------------------------------------------- | 525| progress | number | Yes | No | If the progress indicator provided by the system is not used, the system reports the progress percentage of the copy-and-paste task.| 526 527## ProgressListener<sup>15+</sup> 528 529type ProgressListener = (progress: ProgressInfo) => void 530 531Defines a listener for progress data changes. If the default progress indicator is not used, you can set this API to obtain the paste progress. 532 533**Atomic service API**: This API can be used in atomic services since API version 15. 534 535**System capability**: SystemCapability.MiscServices.Pasteboard 536 537| Name | Type | Mandatory| Description | 538| -------- | ------------------------------- | ---- | ------------------------------------------------------------ | 539| progress | [ProgressInfo](#progressinfo15) | Yes | Defines the progress information. This information is reported only when [ProgressIndicator](#progressindicator15) is set to **NONE**.| 540 541## ProgressSignal<sup>15+</sup> 542 543Defines a function for canceling the paste task. This parameter is valid only when [ProgressIndicator](#progressindicator15) is set to **NONE**. 544 545**System capability**: SystemCapability.MiscServices.Pasteboard 546 547### cancel<sup>15+</sup> 548 549cancel(): void 550 551Cancels an ongoing copy-and-paste task. 552 553**Atomic service API**: This API can be used in atomic services since API version 15. 554 555**System capability**: SystemCapability.MiscServices.Pasteboard 556 557**Example** 558 559```ts 560import { BusinessError, pasteboard } from '@kit.BasicServicesKit'; 561import { fileUri} from '@kit.CoreFileKit'; 562@Entry 563@Component 564struct PasteboardTest { 565 build() { 566 RelativeContainer() { 567 Column() { 568 Column() { 569 Button("Copy txt") 570 .onClick(async ()=>{ 571 let text = "test"; 572 let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text); 573 let systemPasteboard = pasteboard.getSystemPasteboard(); 574 await systemPasteboard.setData(pasteData); 575 let signal = new pasteboard.ProgressSignal; 576 let progressListenerInfo = (progress: pasteboard.ProgressInfo) => { 577 console.info('progressListener success, progress:' + progress.progress); 578 signal.cancel(); 579 }; 580 let destPath: string = '/data/storage/el2/base/files/'; 581 let destUri : string = fileUri.getUriFromPath(destPath); 582 let params: pasteboard.GetDataParams = { 583 destUri: destUri, 584 fileConflictOptions: pasteboard.FileConflictOptions.OVERWRITE, 585 progressIndicator: pasteboard.ProgressIndicator.DEFAULT, 586 progressListener: progressListenerInfo, 587 }; 588 systemPasteboard.getDataWithProgress(params).then((pasteData: pasteboard.PasteData) => { 589 console.error('getDataWithProgress succ'); 590 }).catch((err: BusinessError) => { 591 console.error('Failed to get PasteData. Cause: ' + err.message); 592 }) 593 }) 594 } 595 } 596 } 597 } 598} 599``` 600 601## GetDataParams<sup>15+</sup> 602 603Obtains parameters when an application uses the file copy capability provided by the pasteboard, including the destination path, file conflict options, and progress indicator types. 604 605**Atomic service API**: This API can be used in atomic services since API version 15. 606 607**System capability**: SystemCapability.MiscServices.Pasteboard 608 609| Name | Type | Mandatory| Description | 610| ------------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 611| destUri | string | No | Destination path for copying files. If file processing is not supported, this parameter is not required. If the application involves complex file processing policies or needs to distinguish file multipathing storage, you are advised not to set this parameter but let the application copies files by itself. This parameter is left empty by default.| 612| fileConflictOptions | [FileConflictOptions](#fileconflictoptions15) | No | File conflict options for a copy-and-paste task. The default value is **OVERWRITE**. | 613| progressIndicator | [ProgressIndicator](#progressindicator15) | Yes | Progress indicator options. You can choose whether to use the default progress indicator. | 614| progressListener | [ProgressListener](#progresslistener15) | No | Defines a listener for progress data changes. If the default progress indicator is not used, you can set this type to obtain the paste progress. This parameter is left empty by default.| 615| progressSignal | [ProgressSignal](#progresssignal15) | No | Function for canceling the paste task. This parameter is valid only when [ProgressIndicator](#progressindicator15) is set to **NONE**. This parameter is left empty by default.| 616 617## PasteDataRecord<sup>7+</sup> 618 619Provides **PasteDataRecord** APIs. A **PasteDataRecord** is an abstract definition of the content on the pasteboard. The pasteboard content consists of one or more plain text, HTML, URI, or Want records. 620 621### Attributes 622 623**Atomic service API**: This API can be used in atomic services since API version 11. 624 625**System capability**: SystemCapability.MiscServices.Pasteboard 626 627| Name| Type| Read-Only| Optional| Description| 628| -------- | -------- | -------- | -------- | -------- | 629| htmlText<sup>7+</sup> | string | Yes| No| HTML content.| 630| want<sup>7+</sup> | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| No| Want content.| 631| mimeType<sup>7+</sup> | string | Yes| No| Default data type.| 632| plainText<sup>7+</sup> | string | Yes| No| Plain text.| 633| uri<sup>7+</sup> | string | Yes| No| URI content.| 634| pixelMap<sup>9+</sup> | [image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md) | Yes| No| Pixel map.| 635| data<sup>9+</sup> | {[mimeType: string]: ArrayBuffer} | Yes| No| Content of custom data.| 636 637### toPlainText<sup>9+</sup> 638 639toPlainText(): string 640 641Forcibly converts HTML, plain, and URI content in a **PasteDataRecord** to the plain text. 642 643**Atomic service API**: This API can be used in atomic services since API version 11. 644 645**System capability**: SystemCapability.MiscServices.Pasteboard 646 647**Return value** 648 649| Type| Description| 650| -------- | -------- | 651| string | Plain text.| 652 653**Example** 654 655```ts 656let record: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_HTML, '<html>hello</html>'); 657let text: string = record.toPlainText(); 658console.info(`Succeeded in converting to text. Text: ${text}`); 659``` 660 661### addEntry<sup>14+</sup> 662 663addEntry(type: string, value: ValueType): void 664 665Adds custom data of an extra type to **PasteDataRecord**. The MIME type added using this method is not the default type of **Record**. You can only use the [getData](#getdata14) API to read the corresponding data. 666 667**System capability**: SystemCapability.MiscServices.Pasteboard 668 669**Parameters** 670 671| Name | Type| Mandatory| Description | 672|-------| -------- | -------- |-------------------| 673| type | string | Yes| MIME type of custom data. The value can be a predefined MIME type listed in [Constants](#constants), including HTML, WANT, plain text, URI, and pixel map, or a custom MIME type. The value of **mimeType** cannot exceed 1024 bytes. | 674| value | [ValueType](#valuetype9) | Yes| Content of custom data. | 675 676**Error codes** 677 678For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 679 680| Error Code ID| Error Message| 681| -------- | -------- | 682| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 683 684**Example** 685 686```ts 687let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 688let record: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt'); 689record.addEntry(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 690record.addEntry(pasteboard.MIMETYPE_TEXT_HTML, html); 691``` 692 693### getValidTypes<sup>14+</sup> 694 695getValidTypes(types: Array<string>): Array<string> 696 697Obtains the intersection of the input MIME type and the MIME type of the pasteboard data. 698 699**System capability**: SystemCapability.MiscServices.Pasteboard 700 701**Parameters** 702 703| Name | Type| Mandatory| Description | 704|-------| -------- | -------- |----------------| 705| types | Array<string> | Yes| List of the MIME types.| 706 707**Return value** 708 709| Type| Description | 710| -------- |--------------------------------------| 711| Array<string> | Intersection of the input MIME type and the MIME type of the pasteboard data obtained.| 712 713**Error codes** 714 715For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 716 717| Error Code ID| Error Message| 718| -------- | -------- | 719| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 720 721**Example** 722 723```ts 724let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 725let record: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt'); 726record.addEntry(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 727record.addEntry(pasteboard.MIMETYPE_TEXT_HTML, html); 728let types: string[] = record.getValidTypes([ 729 pasteboard.MIMETYPE_TEXT_PLAIN, 730 pasteboard.MIMETYPE_TEXT_HTML, 731 pasteboard.MIMETYPE_TEXT_URI, 732 pasteboard.MIMETYPE_TEXT_WANT, 733 pasteboard.MIMETYPE_PIXELMAP 734]); 735``` 736 737### getData<sup>14+</sup> 738 739getData(type: string): Promise<ValueType> 740 741Obtains custom data of the specified MIME type from **PasteDataRecord**. 742 743**System capability**: SystemCapability.MiscServices.Pasteboard 744 745**Parameters** 746 747| Name | Type |Mandatory| Description | 748|------|--------|-------- |----------| 749| type | string |Yes| MIME type.| 750 751**Return value** 752 753| Type | Description | 754|-----------------------------------------|----------------------------------------------------------------------------------------------------------------------| 755| Promise<[ValueType](#valuetype9)> | Promise used to return the custom data of the specified MIME type.<br>If **PasteDataRecord** contains data of multiple MIME types, the non-**PasteDataRecord** data of the default MIME type can be obtained only through this API.| 756 757**Error codes** 758 759For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 760 761| Error Code ID| Error Message| 762| -------- | -------- | 763| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 764 765**Example** 766 767```ts 768import { BusinessError } from '@kit.BasicServicesKit'; 769 770let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 771let record: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt'); 772record.addEntry(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 773record.addEntry(pasteboard.MIMETYPE_TEXT_HTML, html); 774record.getData(pasteboard.MIMETYPE_TEXT_PLAIN).then((value: pasteboard.ValueType) => { 775 let textPlainContent = value as string; 776 console.info('Success to get text/plain value. value is: ' + textPlainContent); 777}).catch((err: BusinessError) => { 778 console.error('Failed to get text/plain value. Cause: ' + err.message); 779}); 780record.getData(pasteboard.MIMETYPE_TEXT_URI).then((value: pasteboard.ValueType) => { 781 let uri = value as string; 782 console.info('Success to get text/uri value. value is: ' + uri); 783}).catch((err: BusinessError) => { 784 console.error('Failed to get text/uri value. Cause: ' + err.message); 785}); 786``` 787 788### convertToText<sup>(deprecated)</sup> 789 790convertToText(callback: AsyncCallback<string>): void 791 792Forcibly converts the content in a **PasteData** object to text. This API uses an asynchronous callback to return the result. 793> **NOTE** 794> 795> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [toPlainText](#toplaintext9) instead. 796 797**System capability**: SystemCapability.MiscServices.Pasteboard 798 799**Parameters** 800 801| Name| Type| Mandatory| Description| 802| -------- | -------- | -------- | -------- | 803| callback | AsyncCallback<string> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the text obtained from the conversion. Otherwise, **err** is error information.| 804 805**Error codes** 806 807For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 808 809| Error Code ID| Error Message| 810| -------- | -------- | 811| 401 | Possible causes: Incorrect parameters types. | 812 813**Example** 814 815```ts 816import { BusinessError } from '@kit.BasicServicesKit'; 817 818let record: pasteboard.PasteDataRecord = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt'); 819record.convertToText((err: BusinessError, data: string) => { 820 if (err) { 821 console.error(`Failed to convert to text. Cause: ${err.message}`); 822 return; 823 } 824 console.info(`Succeeded in converting to text. Data: ${data}`); 825}); 826``` 827 828### convertToText<sup>(deprecated)</sup> 829 830convertToText(): Promise<string> 831 832Forcibly converts the content in a **PasteData** object to text. This API uses a promise to return the result. 833> **NOTE** 834> 835> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [toPlainText](#toplaintext9) instead. 836 837**System capability**: SystemCapability.MiscServices.Pasteboard 838 839**Return value** 840 841| Type| Description| 842| -------- | -------- | 843| Promise<string> | Promise used to return the text obtained from the conversion.| 844 845**Example** 846 847```ts 848import { BusinessError } from '@kit.BasicServicesKit'; 849 850let record: pasteboard.PasteDataRecord = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt'); 851record.convertToText().then((data: string) => { 852 console.info(`Succeeded in converting to text. Data: ${data}`); 853}).catch((err: BusinessError) => { 854 console.error(`Failed to convert to text. Cause: ${err.message}`); 855}); 856``` 857 858## PasteData 859 860Implements a **PasteData** object. Paste data contains one or more data records ([PasteDataRecord](#pastedatarecord7)) and property description objects ([PasteDataProperty](#pastedataproperty7)). 861 862Before calling any API in **PasteData**, you must use **[createData()](#pasteboardcreatedata9)** or **[getData()](#getdata9)** to create a **PasteData** object. 863 864**System capability**: SystemCapability.MiscServices.Pasteboard 865 866### getPrimaryText 867 868getPrimaryText(): string 869 870Obtains the plain text of the primary record. 871 872**Atomic service API**: This API can be used in atomic services since API version 11. 873 874**System capability**: SystemCapability.MiscServices.Pasteboard 875 876**Return value** 877 878| Type| Description| 879| -------- | -------- | 880| string | Plain text. If the **PasteData** object does not contain plain text, **undefined** is returned by default.| 881 882**Example** 883 884```ts 885import { BusinessError } from '@kit.BasicServicesKit'; 886 887let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 888systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 889 let text: string = pasteData.getPrimaryText(); 890}).catch((err: BusinessError) => { 891 console.error('Failed to get PasteData. Cause: ' + err.message); 892}); 893``` 894 895### getPrimaryHtml<sup>7+</sup> 896 897getPrimaryHtml(): string 898 899Obtains the HTML content of the primary record. 900 901**Atomic service API**: This API can be used in atomic services since API version 11. 902 903**System capability**: SystemCapability.MiscServices.Pasteboard 904 905**Return value** 906 907| Type| Description| 908| -------- | -------- | 909| string | HTML content. If the **PasteData** object does not contain HTML content, **undefined** is returned by default.| 910 911**Example** 912 913```ts 914import { BusinessError } from '@kit.BasicServicesKit'; 915 916let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 917systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 918 let htmlText: string = pasteData.getPrimaryHtml(); 919}).catch((err: BusinessError) => { 920 console.error('Failed to get PasteData. Cause: ' + err.message); 921}); 922``` 923 924### getPrimaryWant<sup>7+</sup> 925 926getPrimaryWant(): Want 927 928Obtains the **Want** object of the primary record. 929 930**Atomic service API**: This API can be used in atomic services since API version 11. 931 932**System capability**: SystemCapability.MiscServices.Pasteboard 933 934**Return value** 935 936| Type| Description| 937| -------- | -------- | 938| [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Want object. If the **PasteData** object does not contain the **Want** object, **undefined** is returned by default.| 939 940**Example** 941 942```ts 943import { Want } from '@kit.AbilityKit'; 944import { BusinessError } from '@kit.BasicServicesKit'; 945 946let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 947systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 948 let want: Want = pasteData.getPrimaryWant(); 949}).catch((err: BusinessError) => { 950 console.error('Failed to get PasteData. Cause: ' + err.message); 951}); 952``` 953 954### getPrimaryUri<sup>7+</sup> 955 956getPrimaryUri(): string 957 958Obtains the URI of the primary record. 959 960**Atomic service API**: This API can be used in atomic services since API version 11. 961 962**System capability**: SystemCapability.MiscServices.Pasteboard 963 964**Return value** 965 966| Type| Description| 967| -------- | -------- | 968| string | URI content. If the **PasteData** object does not contain the URI, **undefined** is returned by default.| 969 970**Example** 971 972```ts 973import { BusinessError } from '@kit.BasicServicesKit'; 974 975let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 976systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 977 let uri: string = pasteData.getPrimaryUri(); 978}).catch((err: BusinessError) => { 979 console.error('Failed to get PasteData. Cause: ' + err.message); 980}); 981``` 982 983### getPrimaryPixelMap<sup>9+</sup> 984 985getPrimaryPixelMap(): image.PixelMap 986 987Obtains the pixel map of the primary record. 988 989**Atomic service API**: This API can be used in atomic services since API version 11. 990 991**System capability**: SystemCapability.MiscServices.Pasteboard 992 993**Return value** 994 995| Type| Description| 996| -------- | -------- | 997| [image.PixelMap](../apis-image-kit/arkts-apis-image-PixelMap.md) | Pixel map. If the **PasteData** object does not contain the pixel map, **undefined** is returned by default.| 998 999**Example** 1000 1001```ts 1002import { image } from '@kit.ImageKit'; 1003 1004let buffer = new ArrayBuffer(128); 1005let realSize: image.Size = { height: 3, width: 5 }; 1006let opt: image.InitializationOptions = { 1007 size: realSize, 1008 pixelFormat: 3, 1009 editable: true, 1010 alphaType: 1, 1011 scaleMode: 1 1012}; 1013image.createPixelMap(buffer, opt).then((pixelMap: image.PixelMap) => { 1014 let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_PIXELMAP, pixelMap); 1015 let PixelMap: image.PixelMap = pasteData.getPrimaryPixelMap(); 1016}); 1017``` 1018 1019### addRecord<sup>7+</sup> 1020 1021addRecord(record: PasteDataRecord): void 1022 1023Adds a data record to this pasteboard, and adds its type to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 1024 1025**Atomic service API**: This API can be used in atomic services since API version 11. 1026 1027**System capability**: SystemCapability.MiscServices.Pasteboard 1028 1029**Parameters** 1030 1031| Name| Type| Mandatory| Description| 1032| -------- | -------- | -------- | -------- | 1033| record | [PasteDataRecord](#pastedatarecord7) | Yes| Record to add.| 1034 1035**Example** 1036 1037```ts 1038let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt'); 1039let textRecord: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1040let html: string = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 1041let htmlRecord: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_HTML, html); 1042pasteData.addRecord(textRecord); 1043pasteData.addRecord(htmlRecord); 1044``` 1045### addRecord<sup>9+</sup> 1046 1047addRecord(mimeType: string, value: ValueType): void 1048 1049Adds a data record to this pasteboard, and adds its type to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 1050 1051**Atomic service API**: This API can be used in atomic services since API version 11. 1052 1053**System capability**: SystemCapability.MiscServices.Pasteboard 1054 1055**Parameters** 1056 1057| Name| Type| Mandatory| Description| 1058| -------- | -------- | -------- | -------- | 1059| mimeType | string | Yes| MIME type of pasteboard data. The length cannot exceed 1024 bytes.| 1060| value | [ValueType](#valuetype9) | Yes| Data content.| 1061 1062**Error codes** 1063 1064For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1065 1066| Error Code ID| Error Message| 1067| -------- | -------- | 1068| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1069 1070**Example** 1071 1072 ```ts 1073 let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt'); 1074 let dataXml = new ArrayBuffer(256); 1075 pasteData.addRecord('app/xml', dataXml); 1076 ``` 1077 1078### getMimeTypes<sup>7+</sup> 1079 1080getMimeTypes(): Array<string> 1081 1082Obtains a list of **mimeTypes** objects in [PasteDataProperty](#pastedataproperty7) from this pasteboard. If the pasteboard is empty, the returned list is also empty. 1083 1084**Atomic service API**: This API can be used in atomic services since API version 11. 1085 1086**System capability**: SystemCapability.MiscServices.Pasteboard 1087 1088**Return value** 1089 1090| Type| Description| 1091| -------- | -------- | 1092| Array<string> | Non-repeating data types of the data records on the pasteboard.| 1093 1094**Example** 1095 1096```ts 1097let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1098let types: string[] = pasteData.getMimeTypes(); 1099``` 1100 1101### getPrimaryMimeType<sup>7+</sup> 1102 1103getPrimaryMimeType(): string 1104 1105Obtains the data type of the primary record in this pasteboard. 1106 1107**Atomic service API**: This API can be used in atomic services since API version 11. 1108 1109**System capability**: SystemCapability.MiscServices.Pasteboard 1110 1111**Return value** 1112 1113| Type| Description| 1114| -------- | -------- | 1115| string | Data type of the primary record.| 1116 1117**Example** 1118 1119```ts 1120let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1121let type: string = pasteData.getPrimaryMimeType(); 1122``` 1123 1124### getProperty<sup>7+</sup> 1125 1126getProperty(): PasteDataProperty 1127 1128Obtains the property of the pasteboard data. 1129 1130**Atomic service API**: This API can be used in atomic services since API version 11. 1131 1132**System capability**: SystemCapability.MiscServices.Pasteboard 1133 1134**Return value** 1135 1136| Type| Description| 1137| -------- | -------- | 1138| [PasteDataProperty](#pastedataproperty7) | Property of the pasteboard data.| 1139 1140**Example** 1141 1142```ts 1143let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1144let property: pasteboard.PasteDataProperty = pasteData.getProperty(); 1145``` 1146 1147### setProperty<sup>9+</sup> 1148 1149setProperty(property: PasteDataProperty): void 1150 1151Sets a [PasteDataProperty](#pastedataproperty7) object. 1152 1153**Atomic service API**: This API can be used in atomic services since API version 11. 1154 1155**System capability**: SystemCapability.MiscServices.Pasteboard 1156 1157**Parameters** 1158 1159| Name| Type| Mandatory| Description| 1160| -------- | -------- | -------- | -------- | 1161| property | [PasteDataProperty](#pastedataproperty7) | Yes| Property of the pasteboard data.| 1162 1163**Error codes** 1164 1165For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1166 1167| Error Code ID| Error Message| 1168| -------- | -------- | 1169| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1170 1171**Example** 1172 1173```ts 1174type AdditionType = Record<string, Record<string, Object>>; 1175 1176let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_HTML, 'application/xml'); 1177let prop: pasteboard.PasteDataProperty = pasteData.getProperty(); 1178prop.shareOption = pasteboard.ShareOption.INAPP; 1179// Note that attributes cannot be added to additions. Attributes can be added only by re-assigning values. 1180prop.additions = { 'TestOne': { 'Test': 123 }, 'TestTwo': { 'Test': 'additions' } } as AdditionType; 1181prop.tag = 'TestTag'; 1182pasteData.setProperty(prop); 1183``` 1184The **localOnly** and **shareOption** attributes of [PasteDataProperty](#pastedataproperty7) are mutually exclusive. The **shareOption** attribute is prioritized, and its value affects the value of **localOnly**. 1185```ts 1186(async () => { 1187 let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1188 let prop: pasteboard.PasteDataProperty = pasteData.getProperty(); 1189 prop.shareOption = pasteboard.ShareOption.INAPP; 1190 prop.localOnly = false; 1191 pasteData.setProperty(prop); 1192 let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1193 1194 await systemPasteboard.setData(pasteData).then(async () => { 1195 console.info('Succeeded in setting PasteData.'); 1196 await systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 1197 let prop: pasteboard.PasteDataProperty = pasteData.getProperty(); 1198 prop.localOnly; // true 1199 }); 1200 }); 1201 1202 prop.shareOption = pasteboard.ShareOption.LOCALDEVICE; 1203 prop.localOnly = false; 1204 pasteData.setProperty(prop); 1205 1206 await systemPasteboard.setData(pasteData).then(async () => { 1207 console.info('Succeeded in setting PasteData.'); 1208 await systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 1209 let prop: pasteboard.PasteDataProperty = pasteData.getProperty(); 1210 prop.localOnly; // true 1211 }); 1212 }); 1213}) 1214``` 1215 1216### getRecord<sup>9+</sup> 1217 1218getRecord(index: number): PasteDataRecord 1219 1220Obtains the record with a specific index from the pasteboard. 1221 1222**Atomic service API**: This API can be used in atomic services since API version 11. 1223 1224**System capability**: SystemCapability.MiscServices.Pasteboard 1225 1226**Parameters** 1227 1228| Name| Type| Mandatory| Description| 1229| -------- | -------- | -------- | -------- | 1230| index | number | Yes| Index of the target record.| 1231 1232**Return value** 1233 1234| Type| Description| 1235| -------- | -------- | 1236| [PasteDataRecord](#pastedatarecord7) | Record with the specified index.| 1237 1238**Error codes** 1239 1240For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1241 1242| Error Code ID| Error Message| 1243| -------- | -------- | 1244| 12900001 | The index is out of the record. | 1245| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1246 1247**Example** 1248 1249```ts 1250let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1251let record: pasteboard.PasteDataRecord = pasteData.getRecord(0); 1252``` 1253 1254### getRecordCount<sup>7+</sup> 1255 1256getRecordCount(): number 1257 1258Obtains the number of records in the pasteboard. 1259 1260**Atomic service API**: This API can be used in atomic services since API version 11. 1261 1262**System capability**: SystemCapability.MiscServices.Pasteboard 1263 1264**Return value** 1265 1266| Type| Description| 1267| -------- | -------- | 1268| number | Number of records.| 1269 1270**Example** 1271 1272```ts 1273let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1274let count: number = pasteData.getRecordCount(); 1275``` 1276 1277### getTag<sup>7+</sup> 1278 1279getTag(): string 1280 1281Obtains the custom tag from the pasteboard. If no custom tag is set, null is returned. 1282 1283**Atomic service API**: This API can be used in atomic services since API version 11. 1284 1285**System capability**: SystemCapability.MiscServices.Pasteboard 1286 1287**Return value** 1288 1289| Type| Description| 1290| -------- | -------- | 1291| string | Custom tag. If no custom tag is set, null is returned.| 1292 1293**Example** 1294 1295```ts 1296let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1297let tag: string = pasteData.getTag(); 1298``` 1299 1300### hasType<sup>9+</sup> 1301 1302hasType(mimeType: string): boolean 1303 1304Checks whether the pasteboard contains data of the specified type. 1305 1306**Atomic service API**: This API can be used in atomic services since API version 11. 1307 1308**System capability**: SystemCapability.MiscServices.Pasteboard 1309 1310**Parameters** 1311 1312| Name| Type| Mandatory| Description| 1313| -------- | -------- | -------- | -------- | 1314| mimeType | string | Yes| Type of the data to query. The value can be a predefined type listed in Constants(#constants), including HTML, WANT, plain text, URI, and pixel map, or a custom MIME type.| 1315 1316**Return value** 1317 1318| Type| Description| 1319| -------- | -------- | 1320| boolean | Returns **true** if the specified data type exists; returns **false** otherwise.| 1321 1322**Error codes** 1323 1324For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1325 1326| Error Code ID| Error Message| 1327| -------- | -------- | 1328| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1329 1330**Example** 1331 1332```ts 1333let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1334let hasType: boolean = pasteData.hasType(pasteboard.MIMETYPE_TEXT_PLAIN); 1335``` 1336 1337### removeRecord<sup>9+</sup> 1338 1339removeRecord(index: number): void 1340 1341Removes the record with a specific index from the pasteboard. 1342 1343**Atomic service API**: This API can be used in atomic services since API version 11. 1344 1345**System capability**: SystemCapability.MiscServices.Pasteboard 1346 1347**Parameters** 1348 1349| Name| Type| Mandatory| Description| 1350| -------- | -------- | -------- | -------- | 1351| index | number | Yes| Specified index.| 1352 1353**Error codes** 1354 1355For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1356 1357| Error Code ID| Error Message| 1358| -------- | -------- | 1359| 12900001 | The index is out of the record. | 1360| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1361 1362**Example** 1363 1364```ts 1365let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1366pasteData.removeRecord(0); 1367``` 1368 1369### replaceRecord<sup>9+</sup> 1370 1371replaceRecord(index: number, record: PasteDataRecord): void 1372 1373Replaces the record with a specific index from the pasteboard. 1374 1375**Atomic service API**: This API can be used in atomic services since API version 11. 1376 1377**System capability**: SystemCapability.MiscServices.Pasteboard 1378 1379**Parameters** 1380 1381| Name| Type| Mandatory| Description| 1382| -------- | -------- | -------- | -------- | 1383| index | number | Yes| Specified index.| 1384| record | [PasteDataRecord](#pastedatarecord7) | Yes| New record.| 1385 1386**Error codes** 1387 1388For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1389 1390| Error Code ID| Error Message| 1391| -------- | -------- | 1392| 12900001 | The index is out of the record. | 1393| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1394 1395**Example** 1396 1397```ts 1398let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 1399let record: pasteboard.PasteDataRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'file://com.example.myapplication1/data/storage/el2/base/files/file.txt'); 1400pasteData.replaceRecord(0, record); 1401``` 1402 1403### pasteStart<sup>12+</sup> 1404 1405pasteStart(): void 1406 1407Notifies the clipboard service to retain the cross-device channel before reading data from the clipboard. 1408 1409**System capability**: SystemCapability.MiscServices.Pasteboard 1410 1411**Example** 1412 1413```ts 1414import { BusinessError } from '@kit.BasicServicesKit'; 1415 1416let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1417systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => { 1418 if (err) { 1419 console.error('Failed to get PasteData. Cause: ' + err.message); 1420 return; 1421 } 1422 pasteData.pasteStart(); 1423 console.info(`using data: ${pasteData.getPrimaryText()}`); 1424 pasteData.pasteComplete(); 1425}); 1426``` 1427 1428### pasteComplete<sup>12+</sup> 1429 1430pasteComplete(): void 1431 1432Notifies the clipboard service that the paste is complete. 1433 1434**System capability**: SystemCapability.MiscServices.Pasteboard 1435 1436**Example** 1437 1438```ts 1439import { BusinessError } from '@kit.BasicServicesKit'; 1440 1441let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1442systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => { 1443 if (err) { 1444 console.error('Failed to get PasteData. Cause: ' + err.message); 1445 return; 1446 } 1447 pasteData.pasteStart(); 1448 console.info(`using data: ${pasteData.getPrimaryText()}`); 1449 pasteData.pasteComplete(); 1450}); 1451``` 1452 1453### addHtmlRecord<sup>(deprecated)</sup> 1454 1455addHtmlRecord(htmlText: string): void 1456 1457Adds an HTML record to this pasteboard, and adds **MIMETYPE_TEXT_HTML** to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 1458 1459> **NOTE** 1460> 1461> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [addRecord](#addrecord9) instead. 1462 1463**System capability**: SystemCapability.MiscServices.Pasteboard 1464 1465**Parameters** 1466 1467| Name| Type| Mandatory| Description| 1468| -------- | -------- | -------- | -------- | 1469| htmlText | string | Yes| HTML content.| 1470 1471**Example** 1472 1473```ts 1474let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1475let html: string = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>"; 1476pasteData.addHtmlRecord(html); 1477``` 1478 1479### addWantRecord<sup>(deprecated)</sup> 1480 1481addWantRecord(want: Want): void 1482 1483Adds a Want record to this pasteboard, and adds **MIMETYPE_TEXT_WANT** to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 1484 1485> **NOTE** 1486> 1487> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [addRecord](#addrecord9) instead. 1488 1489**System capability**: SystemCapability.MiscServices.Pasteboard 1490 1491**Parameters** 1492 1493| Name| Type| Mandatory| Description| 1494| -------- | -------- | -------- | -------- | 1495| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes| Want object.| 1496 1497**Example** 1498 1499```ts 1500import { Want } from '@kit.AbilityKit'; 1501 1502let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1503let object: Want = { 1504 bundleName: "com.example.aafwk.test", 1505 abilityName: "com.example.aafwk.test.TwoAbility" 1506}; 1507pasteData.addWantRecord(object); 1508``` 1509 1510### addTextRecord<sup>(deprecated)</sup> 1511 1512addTextRecord(text: string): void 1513 1514Adds a plain text record to this pasteboard, and adds **MIME_TEXT_PLAIN** to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 1515 1516> **NOTE** 1517> 1518> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [addRecord](#addrecord9) instead. 1519 1520**System capability**: SystemCapability.MiscServices.Pasteboard 1521 1522**Parameters** 1523 1524| Name| Type| Mandatory| Description| 1525| -------- | -------- | -------- | -------- | 1526| text | string | Yes| Plain text.| 1527 1528**Example** 1529 1530```ts 1531let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1532pasteData.addTextRecord('good'); 1533``` 1534 1535### addUriRecord<sup>(deprecated)</sup> 1536 1537addUriRecord(uri: string): void 1538 1539Adds a URI record to this pasteboard, and adds **MIMETYPE_TEXT_URI** to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 1540 1541> **NOTE** 1542> 1543> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [addRecord](#addrecord9) instead. 1544 1545**System capability**: SystemCapability.MiscServices.Pasteboard 1546 1547**Parameters** 1548 1549| Name| Type| Mandatory| Description| 1550| -------- | -------- | -------- | -------- | 1551| uri | string | Yes| URI content.| 1552 1553**Example** 1554 1555```ts 1556let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1557pasteData.addUriRecord('dataability:///com.example.myapplication1/user.txt'); 1558``` 1559### getRecordAt<sup>(deprecated)</sup> 1560 1561getRecordAt(index: number): PasteDataRecord 1562 1563Obtains the record with a specific index from the pasteboard. 1564> **NOTE** 1565> 1566> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [getRecord](#getrecord9) instead. 1567 1568**System capability**: SystemCapability.MiscServices.Pasteboard 1569 1570**Parameters** 1571 1572| Name| Type| Mandatory| Description| 1573| -------- | -------- | -------- | -------- | 1574| index | number | Yes| Index of the target record.| 1575 1576**Return value** 1577 1578| Type| Description| 1579| -------- | -------- | 1580| [PasteDataRecord](#pastedatarecord7) | Record with the specified index.| 1581 1582**Error codes** 1583 1584For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1585 1586| Error Code ID| Error Message| 1587| -------- | -------- | 1588| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1589 1590**Example** 1591 1592```ts 1593let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1594let record: pasteboard.PasteDataRecord = pasteData.getRecordAt(0); 1595``` 1596 1597### hasMimeType<sup>(deprecated)</sup> 1598 1599hasMimeType(mimeType: string): boolean 1600 1601Checks whether the pasteboard contains data of the specified type. 1602> **NOTE** 1603> 1604> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [hasType](#hastype9) instead. 1605 1606**System capability**: SystemCapability.MiscServices.Pasteboard 1607 1608**Parameters** 1609 1610| Name| Type| Mandatory| Description| 1611| -------- | -------- | -------- | -------- | 1612| mimeType | string | Yes| Type of the data to query.| 1613 1614**Return value** 1615 1616| Type| Description| 1617| -------- | -------- | 1618| boolean | Returns **true** if the specified data type exists; returns **false** otherwise.| 1619 1620**Error codes** 1621 1622For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1623 1624| Error Code ID| Error Message| 1625| -------- | -------- | 1626| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1627 1628**Example** 1629 1630```ts 1631let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1632let hasType: boolean = pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_PLAIN); 1633``` 1634### removeRecordAt<sup>(deprecated)</sup> 1635 1636removeRecordAt(index: number): boolean 1637 1638Removes the record with a specific index from the pasteboard. 1639> **NOTE** 1640> 1641> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [removeRecord](#removerecord9) instead. 1642 1643**System capability**: SystemCapability.MiscServices.Pasteboard 1644 1645**Parameters** 1646 1647| Name| Type| Mandatory| Description| 1648| -------- | -------- | -------- | -------- | 1649| index | number | Yes| Specified index.| 1650 1651**Return value** 1652 1653| Type| Description| 1654| -------- | -------- | 1655| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1656 1657**Error codes** 1658 1659For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1660 1661| Error Code ID| Error Message| 1662| -------- | -------- | 1663| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1664 1665**Example** 1666 1667```ts 1668let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1669let isRemove: boolean = pasteData.removeRecordAt(0); 1670``` 1671### replaceRecordAt<sup>(deprecated)</sup> 1672 1673replaceRecordAt(index: number, record: PasteDataRecord): boolean 1674 1675Replaces the record with a specific index from the pasteboard. 1676> **NOTE** 1677> 1678> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [replaceRecord](#replacerecord9) instead. 1679 1680**System capability**: SystemCapability.MiscServices.Pasteboard 1681 1682**Parameters** 1683 1684| Name| Type| Mandatory| Description| 1685| -------- | -------- | -------- | -------- | 1686| index | number | Yes| Specified index.| 1687| record | [PasteDataRecord](#pastedatarecord7) | Yes| New record.| 1688 1689**Return value** 1690 1691| Type| Description| 1692| -------- | -------- | 1693| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1694 1695**Example** 1696 1697```ts 1698let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('hello'); 1699let record: pasteboard.PasteDataRecord = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt'); 1700let isReplace: boolean = pasteData.replaceRecordAt(0, record); 1701``` 1702 1703## SystemPasteboard 1704 1705Provides **SystemPasteboard** APIs. 1706 1707Before calling any **SystemPasteboard** API, you must obtain a **SystemPasteboard** object using [getSystemPasteboard](#pasteboardgetsystempasteboard). 1708 1709```ts 1710let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1711``` 1712 1713### on('update')<sup>7+</sup> 1714 1715on(type: 'update', callback: () =>void): void 1716 1717Subscribes to the content change event of the system pasteboard. 1718 1719**System capability**: SystemCapability.MiscServices.Pasteboard 1720 1721**Parameters** 1722 1723| Name| Type| Mandatory| Description| 1724| -------- | -------- | -------- | -------- | 1725| type | string | Yes| Event type. The value **'update'** indicates changes in the pasteboard content.| 1726| callback | function | Yes| Callback invoked when the pasteboard content changes.| 1727 1728**Error codes** 1729 1730For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1731 1732| Error Code ID| Error Message| 1733| -------- | -------- | 1734| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1735 1736**Example** 1737 1738```ts 1739let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1740let listener = () => { 1741 console.info('The system pasteboard has changed.'); 1742}; 1743systemPasteboard.on('update', listener); 1744``` 1745 1746### off('update')<sup>7+</sup> 1747 1748off(type: 'update', callback?: () =>void): void 1749 1750Unsubscribes from the system pasteboard content change event. 1751 1752**System capability**: SystemCapability.MiscServices.Pasteboard 1753 1754**Parameters** 1755 1756| Name| Type| Mandatory| Description | 1757| -------- | -------- | -------- |---------------------------------------------------------| 1758| type | string | Yes| Event type. The value **'update'** indicates changes in the pasteboard content. | 1759| callback | function | No| Callback invoked when the pasteboard content changes. If this parameter is not specified, listening will be disabled for all callbacks registered by the current application.| 1760 1761**Error codes** 1762 1763For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1764 1765| Error Code ID| Error Message| 1766| -------- | -------- | 1767| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1768 1769**Example** 1770 1771```ts 1772let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1773let listener = () => { 1774 console.info('The system pasteboard has changed.'); 1775}; 1776systemPasteboard.off('update', listener); 1777``` 1778 1779### clearData<sup>9+</sup> 1780 1781clearData(callback: AsyncCallback<void>): void 1782 1783Clears the system pasteboard. This API uses an asynchronous callback to return the result. 1784 1785**Atomic service API**: This API can be used in atomic services since API version 11. 1786 1787**System capability**: SystemCapability.MiscServices.Pasteboard 1788 1789**Parameters** 1790 1791| Name| Type| Mandatory| Description| 1792| -------- | -------- | -------- | -------- | 1793| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1794 1795**Error codes** 1796 1797For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1798 1799| Error Code ID| Error Message| 1800| -------- | -------- | 1801| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1802 1803**Example** 1804 1805```ts 1806let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1807systemPasteboard.clearData((err, data) => { 1808 if (err) { 1809 console.error(`Failed to clear the pasteboard. Cause: ${err.message}`); 1810 return; 1811 } 1812 console.info('Succeeded in clearing the pasteboard.'); 1813}); 1814``` 1815 1816### clearData<sup>9+</sup> 1817 1818clearData(): Promise<void> 1819 1820Clears the system pasteboard. This API uses a promise to return the result. 1821 1822**Atomic service API**: This API can be used in atomic services since API version 11. 1823 1824**System capability**: SystemCapability.MiscServices.Pasteboard 1825 1826**Return value** 1827 1828| Type| Description| 1829| -------- | -------- | 1830| Promise<void> | Promise that returns no value.| 1831 1832**Example** 1833 1834```ts 1835import { BusinessError } from '@kit.BasicServicesKit'; 1836 1837let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1838systemPasteboard.clearData().then((data: void) => { 1839 console.info('Succeeded in clearing the pasteboard.'); 1840}).catch((err: BusinessError) => { 1841 console.error(`Failed to clear the pasteboard. Cause: ${err.message}`); 1842}); 1843``` 1844 1845### setData<sup>9+</sup> 1846 1847setData(data: PasteData, callback: AsyncCallback<void>): void 1848 1849Writes a **PasteData** object to the pasteboard. This API uses an asynchronous callback to return the result. 1850 1851**Atomic service API**: This API can be used in atomic services since API version 11. 1852 1853**System capability**: SystemCapability.MiscServices.Pasteboard 1854 1855**Parameters** 1856 1857| Name| Type| Mandatory| Description| 1858| -------- | -------- | -------- | -------- | 1859| data | [PasteData](#pastedata) | Yes| **PasteData** object.| 1860| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1861 1862**Error codes** 1863 1864For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1865 1866| Error Code ID| Error Message| 1867| -------- | -------- | 1868| 27787277 | Another copy or paste operation is in progress. | 1869| 27787278 | Replication is prohibited. | 1870| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1871 1872**Example** 1873 1874```ts 1875let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'content'); 1876let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1877systemPasteboard.setData(pasteData, (err, data) => { 1878 if (err) { 1879 console.error('Failed to set PasteData. Cause: ' + err.message); 1880 return; 1881 } 1882 console.info('Succeeded in setting PasteData.'); 1883}); 1884``` 1885 1886### setData<sup>9+</sup> 1887 1888setData(data: PasteData): Promise<void> 1889 1890Writes a **PasteData** object to the pasteboard. This API uses a promise to return the result. 1891 1892**Atomic service API**: This API can be used in atomic services since API version 11. 1893 1894**System capability**: SystemCapability.MiscServices.Pasteboard 1895 1896**Parameters** 1897 1898| Name| Type| Mandatory| Description| 1899| -------- | -------- | -------- | -------- | 1900| data | [PasteData](#pastedata) | Yes| **PasteData** object.| 1901 1902**Return value** 1903 1904| Type| Description| 1905| -------- | -------- | 1906| Promise<void> | Promise that returns no value.| 1907 1908**Error codes** 1909 1910For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1911 1912| Error Code ID| Error Message| 1913| -------- | -------- | 1914| 27787277 | Another copy or paste operation is in progress. | 1915| 27787278 | Replication is prohibited. | 1916| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1917 1918**Example** 1919 1920```ts 1921import { BusinessError } from '@kit.BasicServicesKit'; 1922 1923let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'content'); 1924let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1925systemPasteboard.setData(pasteData).then((data: void) => { 1926 console.info('Succeeded in setting PasteData.'); 1927}).catch((err: BusinessError) => { 1928 console.error('Failed to set PasteData. Cause: ' + err.message); 1929}); 1930``` 1931 1932### getData<sup>9+</sup> 1933 1934getData(callback: AsyncCallback<PasteData>): void 1935 1936Obtains a **PasteData** object from the pasteboard. This API uses an asynchronous callback to return the result. 1937 1938**Required permissions**: ohos.permission.READ_PASTEBOARD. For details about how to access the pasteboard, see [Requesting Permissions to Access the Pasteboard](../../basic-services/pasteboard/get-pastedata-permission-guidelines.md). 1939 1940**Atomic service API**: This API can be used in atomic services since API version 11. 1941 1942**System capability**: SystemCapability.MiscServices.Pasteboard 1943 1944**Parameters** 1945 1946| Name| Type| Mandatory| Description| 1947| -------- | -------- | -------- | -------- | 1948| callback | AsyncCallback<[PasteData](#pastedata)> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the system pasteboard data. Otherwise, **err** is an error object.| 1949 1950**Error codes** 1951 1952For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1953 1954| Error Code ID| Error Message| 1955| -------- | -------- | 1956| 27787277 | Another copy or paste operation is in progress. | 1957| 201 | Permission verification failed. The application does not have the permission required to call the API. | 1958| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1959 1960**Example** 1961 1962```ts 1963import { BusinessError } from '@kit.BasicServicesKit'; 1964 1965let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 1966systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => { 1967 if (err) { 1968 console.error('Failed to get PasteData. Cause: ' + err.message); 1969 return; 1970 } 1971 let text: string = pasteData.getPrimaryText(); 1972}); 1973``` 1974 1975### getData<sup>9+</sup> 1976 1977getData(): Promise<PasteData> 1978 1979Obtains a **PasteData** object from the pasteboard. This API uses a promise to return the result. 1980 1981**Required permissions**: ohos.permission.READ_PASTEBOARD. For details about how to access the pasteboard, see [Requesting Permissions to Access the Pasteboard](../../basic-services/pasteboard/get-pastedata-permission-guidelines.md). 1982 1983**Atomic service API**: This API can be used in atomic services since API version 11. 1984 1985**System capability**: SystemCapability.MiscServices.Pasteboard 1986 1987**Return value** 1988 1989| Type| Description| 1990| -------- | -------- | 1991| Promise<[PasteData](#pastedata)> | Promise used to return the system pasteboard data.| 1992 1993**Error codes** 1994 1995For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 1996 1997| Error Code ID| Error Message| 1998| -------- | -------- | 1999| 27787277 | Another copy or paste operation is in progress. | 2000| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2001 2002**Example** 2003 2004```ts 2005import { BusinessError } from '@kit.BasicServicesKit'; 2006 2007let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2008systemPasteboard.getData().then((pasteData: pasteboard.PasteData) => { 2009 let text: string = pasteData.getPrimaryText(); 2010}).catch((err: BusinessError) => { 2011 console.error('Failed to get PasteData. Cause: ' + err.message); 2012}); 2013``` 2014 2015### hasData<sup>9+</sup> 2016 2017hasData(callback: AsyncCallback<boolean>): void 2018 2019Checks whether the system pasteboard contains data. This API uses an asynchronous callback to return the result. 2020 2021**Atomic service API**: This API can be used in atomic services since API version 11. 2022 2023**System capability**: SystemCapability.MiscServices.Pasteboard 2024 2025**Parameters** 2026 2027| Name| Type| Mandatory| Description| 2028| -------- | -------- | -------- | -------- | 2029| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 2030 2031**Error codes** 2032 2033For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2034 2035| Error Code ID| Error Message| 2036| -------- | -------- | 2037| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2038 2039**Example** 2040 2041```ts 2042import { BusinessError } from '@kit.BasicServicesKit'; 2043 2044let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2045systemPasteboard.hasData((err: BusinessError, data: boolean) => { 2046 if (err) { 2047 console.error(`Failed to check the PasteData. Cause: ${err.message}`); 2048 return; 2049 } 2050 console.info(`Succeeded in checking the PasteData. Data: ${data}`); 2051}); 2052``` 2053 2054### hasData<sup>9+</sup> 2055 2056hasData(): Promise<boolean> 2057 2058Checks whether the system pasteboard contains data. This API uses a promise to return the result. 2059 2060**Atomic service API**: This API can be used in atomic services since API version 11. 2061 2062**System capability**: SystemCapability.MiscServices.Pasteboard 2063 2064**Return value** 2065 2066| Type| Description| 2067| -------- | -------- | 2068| Promise<boolean> | Callback used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 2069 2070**Example** 2071 2072```ts 2073import { BusinessError } from '@kit.BasicServicesKit'; 2074 2075let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2076systemPasteboard.hasData().then((data: boolean) => { 2077 console.info(`Succeeded in checking the PasteData. Data: ${data}`); 2078}).catch((err: BusinessError) => { 2079 console.error(`Failed to check the PasteData. Cause: ${err.message}`); 2080}); 2081``` 2082 2083### clear<sup>(deprecated)</sup> 2084 2085clear(callback: AsyncCallback<void>): void 2086 2087Clears the system pasteboard. This API uses an asynchronous callback to return the result. 2088> **NOTE** 2089> 2090> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [pasteboard.clearData](#cleardata9) instead. 2091 2092**System capability**: SystemCapability.MiscServices.Pasteboard 2093 2094**Parameters** 2095 2096| Name| Type| Mandatory| Description| 2097| -------- | -------- | -------- | -------- | 2098| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2099 2100**Error codes** 2101 2102For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2103 2104| Error Code ID| Error Message| 2105| -------- | -------- | 2106| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2107 2108**Example** 2109 2110```ts 2111let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2112systemPasteboard.clear((err, data) => { 2113 if (err) { 2114 console.error(`Failed to clear the PasteData. Cause: ${err.message}`); 2115 return; 2116 } 2117 console.info('Succeeded in clearing the PasteData.'); 2118}); 2119``` 2120 2121### clear<sup>(deprecated)</sup> 2122 2123clear(): Promise<void> 2124 2125Clears the system pasteboard. This API uses a promise to return the result. 2126> **NOTE** 2127> 2128> This API is supported since API version 7 and deprecated since API version 9. Use [pasteboard.clearData](#cleardata9-1) instead. 2129 2130**System capability**: SystemCapability.MiscServices.Pasteboard 2131 2132**Return value** 2133 2134| Type| Description| 2135| -------- | -------- | 2136| Promise<void> | Promise that returns no value.| 2137 2138**Example** 2139 2140```ts 2141import { BusinessError } from '@kit.BasicServicesKit'; 2142 2143let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2144systemPasteboard.clear().then((data) => { 2145 console.info('Succeeded in clearing the PasteData.'); 2146}).catch((err: BusinessError) => { 2147 console.error(`Failed to clear the PasteData. Cause: ${err.message}`); 2148}); 2149``` 2150 2151### getPasteData<sup>(deprecated)</sup> 2152 2153getPasteData(callback: AsyncCallback<PasteData>): void 2154 2155Obtains a **PasteData** object from the pasteboard. This API uses an asynchronous callback to return the result. 2156> **NOTE** 2157> 2158> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [getData](#getdata9) instead. 2159 2160**System capability**: SystemCapability.MiscServices.Pasteboard 2161 2162**Parameters** 2163 2164| Name| Type| Mandatory| Description| 2165| -------- | -------- | -------- | -------- | 2166| callback | AsyncCallback<[PasteData](#pastedata)> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the system pasteboard data. Otherwise, **err** is an error object.| 2167 2168**Error codes** 2169 2170For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2171 2172| Error Code ID| Error Message| 2173| -------- | -------- | 2174| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2175 2176**Example** 2177 2178```ts 2179import { BusinessError } from '@kit.BasicServicesKit'; 2180 2181let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2182systemPasteboard.getPasteData((err: BusinessError, pasteData: pasteboard.PasteData) => { 2183 if (err) { 2184 console.error('Failed to get PasteData. Cause: ' + err.message); 2185 return; 2186 } 2187 let text: string = pasteData.getPrimaryText(); 2188}); 2189``` 2190 2191### getPasteData<sup>(deprecated)</sup> 2192 2193getPasteData(): Promise<PasteData> 2194 2195Obtains a **PasteData** object from the pasteboard. This API uses a promise to return the result. 2196> **NOTE** 2197> 2198> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [getData](#getdata9-1) instead. 2199 2200**System capability**: SystemCapability.MiscServices.Pasteboard 2201 2202**Return value** 2203 2204| Type| Description| 2205| -------- | -------- | 2206| Promise<[PasteData](#pastedata)> | Promise used to return the system pasteboard data.| 2207 2208**Example** 2209 2210```ts 2211import { BusinessError } from '@kit.BasicServicesKit'; 2212 2213let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2214systemPasteboard.getPasteData().then((pasteData: pasteboard.PasteData) => { 2215 let text: string = pasteData.getPrimaryText(); 2216}).catch((err: BusinessError) => { 2217 console.error('Failed to get PasteData. Cause: ' + err.message); 2218}); 2219``` 2220 2221### hasPasteData<sup>(deprecated)</sup> 2222 2223hasPasteData(callback: AsyncCallback<boolean>): void 2224 2225Checks whether the system pasteboard contains data. This API uses an asynchronous callback to return the result. 2226> **NOTE** 2227> 2228> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [hasData](#hasdata9) instead. 2229 2230**System capability**: SystemCapability.MiscServices.Pasteboard 2231 2232**Parameters** 2233 2234| Name| Type| Mandatory| Description| 2235| -------- | -------- | -------- | -------- | 2236| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 2237 2238**Error codes** 2239 2240For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2241 2242| Error Code ID| Error Message| 2243| -------- | -------- | 2244| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2245 2246**Example** 2247 2248```ts 2249import { BusinessError } from '@kit.BasicServicesKit'; 2250 2251let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2252systemPasteboard.hasPasteData((err: BusinessError, data: boolean) => { 2253 if (err) { 2254 console.error(`Failed to check the PasteData. Cause: ${err.message}`); 2255 return; 2256 } 2257 console.info(`Succeeded in checking the PasteData. Data: ${data}`); 2258}); 2259``` 2260 2261### hasPasteData<sup>(deprecated)</sup> 2262 2263hasPasteData(): Promise<boolean> 2264 2265Checks whether the system pasteboard contains data. This API uses a promise to return the result. 2266> **NOTE** 2267> 2268> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [hasData](#hasdata9-1) instead. 2269 2270**System capability**: SystemCapability.MiscServices.Pasteboard 2271 2272**Return value** 2273 2274| Type| Description| 2275| -------- | -------- | 2276| Promise<boolean> | Callback used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 2277 2278**Example** 2279 2280```ts 2281import { BusinessError } from '@kit.BasicServicesKit'; 2282 2283let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2284systemPasteboard.hasPasteData().then((data: boolean) => { 2285 console.info(`Succeeded in checking the PasteData. Data: ${data}`); 2286}).catch((err: BusinessError) => { 2287 console.error(`Failed to check the PasteData. Cause: ${err.message}`); 2288}); 2289``` 2290 2291### setPasteData<sup>(deprecated)</sup> 2292 2293setPasteData(data: PasteData, callback: AsyncCallback<void>): void 2294 2295Writes a **PasteData** object to the pasteboard. This API uses an asynchronous callback to return the result. 2296> **NOTE** 2297> 2298> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [setData](#setdata9) instead. 2299 2300**System capability**: SystemCapability.MiscServices.Pasteboard 2301 2302**Parameters** 2303 2304| Name| Type| Mandatory| Description| 2305| -------- | -------- | -------- | -------- | 2306| data | [PasteData](#pastedata) | Yes| **PasteData** object.| 2307| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2308 2309**Error codes** 2310 2311For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2312 2313| Error Code ID| Error Message| 2314| -------- | -------- | 2315| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2316 2317**Example** 2318 2319```ts 2320let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('content'); 2321let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2322systemPasteboard.setPasteData(pasteData, (err, data) => { 2323 if (err) { 2324 console.error('Failed to set PasteData. Cause: ' + err.message); 2325 return; 2326 } 2327 console.info('Succeeded in setting PasteData.'); 2328}); 2329``` 2330### setPasteData<sup>(deprecated)</sup> 2331 2332setPasteData(data: PasteData): Promise<void> 2333 2334Writes a **PasteData** object to the pasteboard. This API uses a promise to return the result. 2335> **NOTE** 2336> 2337> This API is supported since API version 6 and deprecated since API version 9. You are advised to use [setData](#setdata9-1) instead. 2338 2339**System capability**: SystemCapability.MiscServices.Pasteboard 2340 2341**Parameters** 2342 2343| Name| Type| Mandatory| Description| 2344| -------- | -------- | -------- | -------- | 2345| data | [PasteData](#pastedata) | Yes| **PasteData** object.| 2346 2347**Return value** 2348 2349| Type| Description| 2350| -------- | -------- | 2351| Promise<void> | Promise that returns no value.| 2352 2353**Example** 2354 2355```ts 2356import { BusinessError } from '@kit.BasicServicesKit'; 2357 2358let pasteData: pasteboard.PasteData = pasteboard.createPlainTextData('content'); 2359let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2360systemPasteboard.setPasteData(pasteData).then((data: void) => { 2361 console.info('Succeeded in setting PasteData.'); 2362}).catch((err: BusinessError) => { 2363 console.error('Failed to set PasteData. Cause: ' + err.message); 2364}); 2365``` 2366### isRemoteData<sup>11+</sup> 2367 2368isRemoteData(): boolean 2369 2370Checks whether the data in the pasteboard is from another device. 2371 2372**Atomic service API**: This API can be used in atomic services since API version 11. 2373 2374**System capability**: SystemCapability.MiscServices.Pasteboard 2375 2376**Return value** 2377 2378| Type | Description | 2379| ------- | ------------------------------------- | 2380| boolean | Returns **true** if the data in the pasteboard is from another device; returns **false** otherwise.| 2381 2382**Error codes** 2383 2384For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2385 2386| Error Code ID| Error Message| 2387| -------- | -------- | 2388| 12900005 | Excessive processing time for internal data. | 2389 2390**Example** 2391 2392```ts 2393let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2394try { 2395 let result: boolean = systemPasteboard.isRemoteData(); 2396 console.info(`Succeeded in checking the RemoteData. Result: ${result}`); 2397} catch (err) { 2398 console.error('Failed to check the RemoteData. Cause:' + err.message); 2399}; 2400``` 2401 2402### getDataSource<sup>11+</sup> 2403 2404getDataSource(): string 2405 2406Obtains the name of the application that provides data. 2407 2408**Atomic service API**: This API can be used in atomic services since API version 11. 2409 2410**System capability**: SystemCapability.MiscServices.Pasteboard 2411 2412**Return value** 2413 2414| Type | Description | 2415| ------ | ------ | 2416| string | Application name.| 2417 2418**Error codes** 2419 2420For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2421 2422| Error Code ID| Error Message| 2423| -------- | -------- | 2424| 12900005 | Excessive processing time for internal data. | 2425 2426**Example** 2427 2428```ts 2429let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2430try { 2431 let result: string = systemPasteboard.getDataSource(); 2432 console.info(`Succeeded in getting DataSource. Result: ${result}`); 2433} catch (err) { 2434 console.error('Failed to get DataSource. Cause:' + err.message); 2435}; 2436``` 2437 2438### hasDataType<sup>11+</sup> 2439 2440hasDataType(mimeType: string): boolean 2441 2442Checks whether the pasteboard contains data of the specified type. 2443 2444**Atomic service API**: This API can be used in atomic services since API version 11. 2445 2446**System capability**: SystemCapability.MiscServices.Pasteboard 2447 2448**Parameters** 2449 2450| Name | Type | Mandatory| Description | 2451| -------- | ------ | ---- | ------------------ | 2452| mimeType | string | Yes | Data type.| 2453 2454**Return value** 2455 2456| Type | Description | 2457| ------- | ------------------------------------------- | 2458| boolean | Returns **true** if the pasteboard contains data of the specified type; returns **false** otherwise.| 2459 2460**Error codes** 2461 2462For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 2463 2464| Error Code ID| Error Message| 2465| -------- | -------- | 2466| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2467| 12900005 | Excessive processing time for internal data. | 2468 2469**Example** 2470 2471```ts 2472let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2473try { 2474 let result: boolean = systemPasteboard.hasDataType(pasteboard.MIMETYPE_TEXT_PLAIN); 2475 console.info(`Succeeded in checking the DataType. Result: ${result}`); 2476} catch (err) { 2477 console.error('Failed to check the DataType. Cause:' + err.message); 2478}; 2479``` 2480 2481### clearDataSync<sup>11+</sup> 2482 2483clearDataSync(): void 2484 2485Clears the system pasteboard. This API returns the result synchronously. 2486 2487**Atomic service API**: This API can be used in atomic services since API version 11. 2488 2489**System capability**: SystemCapability.MiscServices.Pasteboard 2490 2491**Error codes** 2492 2493For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2494 2495| Error Code ID| Error Message| 2496| -------- | -------- | 2497| 12900005 | Excessive processing time for internal data. | 2498 2499**Example** 2500 2501```ts 2502let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2503try { 2504 systemPasteboard.clearDataSync(); 2505 console.info('Succeeded in clearing the pasteboard.'); 2506} catch (err) { 2507 console.error('Failed to clear the pasteboard. Cause:' + err.message); 2508}; 2509``` 2510 2511### getDataSync<sup>11+</sup> 2512 2513getDataSync(): PasteData 2514 2515Reads data in the system pasteboard. This API returns the result synchronously. 2516 2517**Required permissions**: ohos.permission.READ_PASTEBOARD. For details about how to access the pasteboard, see [Requesting Permissions to Access the Pasteboard](../../basic-services/pasteboard/get-pastedata-permission-guidelines.md). 2518 2519**Atomic service API**: This API can be used in atomic services since API version 11. 2520 2521**System capability**: SystemCapability.MiscServices.Pasteboard 2522 2523**Return value** 2524 2525| Type | Description | 2526| ----------------------- | -------------------- | 2527| [PasteData](#pastedata) | Data in the system pasteboard.| 2528 2529**Error codes** 2530 2531For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 2532 2533| Error Code ID| Error Message| 2534| -------- | -------- | 2535| 12900005 | Excessive processing time for internal data. | 2536| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2537 2538**Example** 2539 2540```ts 2541let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2542try { 2543 let result: pasteboard.PasteData = systemPasteboard.getDataSync(); 2544 console.info('Succeeded in getting PasteData.'); 2545} catch (err) { 2546 console.error('Failed to get PasteData. Cause:' + err.message); 2547}; 2548``` 2549 2550### setDataSync<sup>11+</sup> 2551 2552setDataSync(data: PasteData): void 2553 2554Writes data to the system pasteboard. This API returns the result synchronously. 2555 2556**Atomic service API**: This API can be used in atomic services since API version 11. 2557 2558**System capability**: SystemCapability.MiscServices.Pasteboard 2559 2560**Parameters** 2561 2562| Name| Type | Mandatory| Description | 2563| ------ | ----------------------- | ---- | ---------------- | 2564| data | [PasteData](#pastedata) | Yes | Data to be written to the pasteboard.| 2565 2566**Error codes** 2567 2568For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Pasteboard Error Codes](errorcode-pasteboard.md). 2569 2570| Error Code ID| Error Message| 2571| -------- | -------- | 2572| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2573| 12900005 | Excessive processing time for internal data. | 2574 2575**Example** 2576 2577```ts 2578let pasteData: pasteboard.PasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello'); 2579let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2580try { 2581 systemPasteboard.setDataSync(pasteData); 2582 console.info('Succeeded in setting PasteData.'); 2583} catch (err) { 2584 console.error('Failed to set PasteData. Cause:' + err.message); 2585}; 2586``` 2587 2588### hasDataSync<sup>11+</sup> 2589 2590hasDataSync(): boolean 2591 2592Checks whether the system pasteboard contains data. This API returns the result synchronously. 2593 2594**Atomic service API**: This API can be used in atomic services since API version 11. 2595 2596**System capability**: SystemCapability.MiscServices.Pasteboard 2597 2598**Return value** 2599 2600| Type | Description | 2601| ------- | ----------------------------------------------------------------------- | 2602| boolean | Callback used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 2603 2604**Error codes** 2605 2606For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2607 2608| Error Code ID| Error Message| 2609| -------- | -------- | 2610| 12900005 | Excessive processing time for internal data. | 2611 2612**Example** 2613 2614```ts 2615let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2616try { 2617 let result: boolean = systemPasteboard.hasDataSync(); 2618 console.info(`Succeeded in checking the PasteData. Result: ${result}`); 2619} catch (err) { 2620 console.error('Failed to check the PasteData. Cause:' + err.message); 2621}; 2622``` 2623 2624### getUnifiedData<sup>12+</sup> 2625 2626getUnifiedData(): Promise<unifiedDataChannel.UnifiedData> 2627 2628Obtains a **PasteData** object from the pasteboard. This API uses a promise to return the result. 2629 2630**Required permissions**: ohos.permission.READ_PASTEBOARD. For details about how to access the pasteboard, see [Requesting Permissions to Access the Pasteboard](../../basic-services/pasteboard/get-pastedata-permission-guidelines.md). 2631 2632**Atomic service API**: This API can be used in atomic services since API version 12. 2633 2634**System capability**: SystemCapability.MiscServices.Pasteboard 2635 2636**Return value** 2637 2638| Type| Description| 2639| -------- | -------- | 2640| Promise<[unifiedDataChannel.UnifiedData](../apis-arkdata/js-apis-data-unifiedDataChannel.md#unifieddata)> | Promise used to return the system pasteboard data.| 2641 2642**Error codes** 2643 2644For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2645 2646| Error Code ID| Error Message| 2647| -------- | -------- | 2648| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2649| 27787277 | Another copy or paste operation is in progress. | 2650 2651**Example** 2652 2653```ts 2654import { BusinessError } from '@kit.BasicServicesKit'; 2655import { unifiedDataChannel, uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 2656 2657let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2658systemPasteboard.getUnifiedData().then((data) => { 2659 let records: Array<unifiedDataChannel.UnifiedRecord> = data.getRecords(); 2660 for (let j = 0; j < records.length; j++) { 2661 if (records[j].getType() === uniformTypeDescriptor.UniformDataType.PLAIN_TEXT) { 2662 let text = records[j].getValue() as uniformDataStruct.PlainText; 2663 console.info(`${j + 1}.${text.textContent}`); 2664 } 2665 } 2666}).catch((err: BusinessError) => { 2667 console.error('Failed to get UnifiedData. Cause: ' + err.message); 2668}); 2669``` 2670 2671### getUnifiedDataSync<sup>12+</sup> 2672 2673getUnifiedDataSync(): unifiedDataChannel.UnifiedData 2674 2675Reads data in the system pasteboard. This API returns the result synchronously. 2676 2677**Required permissions**: ohos.permission.READ_PASTEBOARD. For details about how to access the pasteboard, see [Requesting Permissions to Access the Pasteboard](../../basic-services/pasteboard/get-pastedata-permission-guidelines.md). 2678 2679**Atomic service API**: This API can be used in atomic services since API version 12. 2680 2681**System capability**: SystemCapability.MiscServices.Pasteboard 2682 2683**Return value** 2684 2685| Type | Description | 2686| -------------------- | -------------------- | 2687| [unifiedDataChannel.UnifiedData](../apis-arkdata/js-apis-data-unifiedDataChannel.md#unifieddata) | Data in the system pasteboard.| 2688 2689**Error codes** 2690 2691For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2692 2693| Error Code ID| Error Message| 2694| -------- | -------- | 2695| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2696| 12900005 | Excessive processing time for internal data. | 2697 2698**Example** 2699 2700```ts 2701import { unifiedDataChannel } from '@kit.ArkData'; 2702 2703let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2704try { 2705 let result: unifiedDataChannel.UnifiedData = systemPasteboard.getUnifiedDataSync(); 2706 console.info('Succeeded in getting UnifiedData.'); 2707} catch (err) { 2708 console.error('Failed to get UnifiedData. Cause:' + err.message); 2709}; 2710``` 2711 2712### setUnifiedData<sup>12+</sup> 2713 2714setUnifiedData(data: unifiedDataChannel.UnifiedData): Promise<void> 2715 2716Writes a **PasteData** object to the pasteboard. This API uses a promise to return the result. 2717 2718**System capability**: SystemCapability.MiscServices.Pasteboard 2719 2720**Atomic service API**: This API can be used in atomic services since API version 12. 2721 2722**Parameters** 2723 2724| Name| Type| Mandatory| Description| 2725| -------- | -------- | -------- | -------- | 2726| data | [unifiedDataChannel.UnifiedData](../apis-arkdata/js-apis-data-unifiedDataChannel.md#unifieddata) | Yes| Data to be written to the pasteboard.| 2727 2728**Return value** 2729 2730| Type| Description| 2731| -------- | -------- | 2732| Promise<void> | Promise that returns no value.| 2733 2734**Error codes** 2735 2736For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2737 2738| Error Code ID| Error Message| 2739| -------- | -------- | 2740| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2741| 27787277 | Another copy or paste operation is in progress. | 2742| 27787278 | Replication is prohibited. | 2743 2744**Example** 2745 2746```ts 2747import { BusinessError } from '@kit.BasicServicesKit'; 2748import { unifiedDataChannel, uniformDataStruct, uniformTypeDescriptor } from '@kit.ArkData'; 2749 2750let plainText : uniformDataStruct.PlainText = { 2751 uniformDataType: uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, 2752 textContent : 'PLAINTEXT_CONTENT', 2753 abstract : 'PLAINTEXT_ABSTRACT', 2754} 2755let record = new unifiedDataChannel.UnifiedRecord(uniformTypeDescriptor.UniformDataType.PLAIN_TEXT, plainText); 2756let data = new unifiedDataChannel.UnifiedData(); 2757data.addRecord(record); 2758 2759let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2760systemPasteboard.setUnifiedData(data).then((data: void) => { 2761 console.info('Succeeded in setting UnifiedData.'); 2762}).catch((err: BusinessError) => { 2763 console.error('Failed to setUnifiedData. Cause: ' + err.message); 2764}); 2765``` 2766 2767### setUnifiedDataSync<sup>12+</sup> 2768 2769setUnifiedDataSync(data: unifiedDataChannel.UnifiedData): void 2770 2771Writes data to the system pasteboard. This API returns the result synchronously. 2772 2773**System capability**: SystemCapability.MiscServices.Pasteboard 2774 2775**Atomic service API**: This API can be used in atomic services since API version 12. 2776 2777**Parameters** 2778 2779| Name| Type | Mandatory| Description | 2780| ------ | ----------- | ---- | ---------------- | 2781| data | [unifiedDataChannel.UnifiedData](../apis-arkdata/js-apis-data-unifiedDataChannel.md#unifieddata) | Yes | Data to be written to the pasteboard.| 2782 2783**Error codes** 2784 2785For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2786 2787| Error Code ID| Error Message| 2788| -------- | -------- | 2789| 401 | Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2790| 12900005 | Excessive processing time for internal data. | 2791 2792**Example** 2793 2794```ts 2795import { unifiedDataChannel } from '@kit.ArkData'; 2796 2797let plainTextData = new unifiedDataChannel.UnifiedData(); 2798let plainText = new unifiedDataChannel.PlainText(); 2799plainText.details = { 2800 Key: 'delayPlaintext', 2801 Value: 'delayPlaintext', 2802}; 2803plainText.textContent = 'delayTextContent'; 2804plainText.abstract = 'delayTextContent'; 2805plainTextData.addRecord(plainText); 2806 2807let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2808try { 2809 systemPasteboard.setUnifiedDataSync(plainTextData); 2810 console.info('Succeeded in setting UnifiedData.'); 2811} catch (err) { 2812 console.error('Failed to set UnifiedData. Cause:' + err.message); 2813}; 2814``` 2815 2816### setAppShareOptions<sup>14+</sup> 2817 2818setAppShareOptions(shareOptions: ShareOption): void 2819 2820Sets pasteable range of pasteboard data for applications. 2821 2822**Required permissions**: ohos.permission.MANAGE_PASTEBOARD_APP_SHARE_OPTION 2823 2824**System capability**: SystemCapability.MiscServices.Pasteboard 2825 2826**Parameters** 2827 2828| Name| Type| Mandatory| Description| 2829| -------- | -------- | -------- | -------- | 2830| shareOptions | [ShareOption](js-apis-pasteboard.md#shareoption9) | Yes| Pasteable range. Only **pasteboard.ShareOption.INAPP** is allowed.| 2831 2832**Error codes** 2833 2834For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2835 2836| Error Code ID| Error Message| 2837| -------- | -------- | 2838| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2839| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2840| 12900006 | Settings already exist. | 2841 2842**Example** 2843 2844```ts 2845import { BusinessError } from '@kit.BasicServicesKit'; 2846 2847let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2848try { 2849 systemPasteboard.setAppShareOptions(pasteboard.ShareOption.INAPP); 2850 console.info('Set app share options success.'); 2851} catch (err) { 2852 let error: BusinessError = err as BusinessError; 2853 console.error(`Set app share options failed, errorCode: ${error.code}, errorMessage: ${error.message}.`); 2854} 2855``` 2856 2857### removeAppShareOptions<sup>14+</sup> 2858 2859removeAppShareOptions(): void 2860 2861Deletes the global pasteable range of the application. 2862 2863**Required permissions**: ohos.permission.MANAGE_PASTEBOARD_APP_SHARE_OPTION 2864 2865**System capability**: SystemCapability.MiscServices.Pasteboard 2866 2867**Error codes** 2868 2869For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 2870 2871| Error Code ID| Error Message| 2872| -------- | -------- | 2873| 201 | Permission verification failed. The application does not have the permission required to call the API. | 2874 2875**Example** 2876 2877```ts 2878import { BusinessError } from '@kit.BasicServicesKit'; 2879 2880let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2881try { 2882 systemPasteboard.removeAppShareOptions(); 2883 console.info('Remove app share options success.'); 2884} catch (err) { 2885 let error: BusinessError = err as BusinessError; 2886 console.error(`Remove app share options failed, errorCode: ${error.code}, errorMessage: ${error.message}.`); 2887} 2888``` 2889 2890### Pattern<sup>13+</sup> 2891Describes the modes supported by the pasteboard. 2892 2893**System capability**: SystemCapability.MiscServices.Pasteboard 2894 2895| Name | Value | Description | 2896| ---------------------------------- | --- | ------------------------------------------------------------------------------------- | 2897| URL | 0 | URL. | 2898| NUMBER | 1 | Number. | 2899| EMAIL_ADDRESS | 2 | Email address.| 2900 2901### detectPatterns<sup>13+</sup> 2902 2903detectPatterns(patterns: Array<Pattern>): Promise<Array<Pattern>> 2904 2905Detects patterns on the **local** pasteboard. This API uses a promise to return the result. 2906 2907**System capability**: SystemCapability.MiscServices.Pasteboard 2908 2909**Parameters** 2910 2911| Name| Type| Mandatory| Description| 2912| -------- | -------- | -------- | -------- | 2913| patterns | [Array<Pattern>](#pattern13) | Yes| Pattern to be detected in the pasteboard.| 2914 2915**Return value** 2916 2917| Type| Description| 2918| -------- | -------- | 2919| Promise<Array<Pattern>> | Promise used to return the detected pattern.| 2920 2921**Error codes** 2922 2923For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2924 2925| Error Code ID| Error Message| 2926| -------- | -------- | 2927| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. 3. Parameter verification failed. | 2928 2929**Example** 2930 2931```ts 2932import { pasteboard } from '@kit.BasicServicesKit' 2933 2934let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2935let patterns: Array<pasteboard.Pattern> = [pasteboard.Pattern.URL, pasteboard.Pattern.EMAIL_ADDRESS]; 2936 2937systemPasteboard.detectPatterns(patterns).then((data: Array<pasteboard.Pattern>) => { 2938 if (patterns.sort().join('')==data.sort().join('')) { 2939 console.info('All needed patterns detected, next get data'); 2940 try { 2941 let result: pasteboard.PasteData = systemPasteboard.getDataSync(); 2942 console.info('Succeeded in getting PasteData.'); 2943 } catch (err) { 2944 console.error('Failed to get PasteData. Cause:' + err.message); 2945 }; 2946 } else { 2947 console.info("Not all needed patterns detected, no need to get data."); 2948 } 2949}); 2950``` 2951 2952### getMimeTypes<sup>14+</sup> 2953 2954getMimeTypes(): Promise<Array<string>> 2955 2956Obtains the MIME type from the pasteboard. This API uses a promise to return the result. 2957 2958**Atomic service API**: This API can be used in atomic services since API version 14. 2959 2960**System capability**: SystemCapability.MiscServices.Pasteboard 2961 2962**Return value** 2963 2964| Type| Description| 2965| -------- | -------- | 2966| Promise<Array<string>> | Promise used to return the read MIME type.| 2967 2968**Example** 2969 2970```ts 2971import { pasteboard, BusinessError } from '@kit.BasicServicesKit' 2972 2973let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 2974systemPasteboard.getMimeTypes().then((data: Array<String>) => { 2975 console.info('Succeeded in getting mimeTypes. mimeTypes: ' + data.sort().join(',')); 2976}).catch((err: BusinessError) => { 2977 console.error('Failed to get mimeTypes. Cause:' + err.message); 2978}); 2979``` 2980 2981### getDataWithProgress<sup>15+</sup> 2982 2983getDataWithProgress(params: GetDataParams): Promise<PasteData> 2984 2985Obtains the pasteboard data and progress. This API uses a promise to return the result. Folders cannot be copied. 2986 2987**Required permissions**: ohos.permission.READ_PASTEBOARD. For details about how to access the pasteboard, see [Requesting Permissions to Access the Pasteboard](../../basic-services/pasteboard/get-pastedata-permission-guidelines.md). 2988 2989**Atomic service API**: This API can be used in atomic services since API version 15. 2990 2991**System capability**: SystemCapability.MiscServices.Pasteboard 2992 2993**Parameters** 2994 2995| Name| Type | Mandatory| Description | 2996| ------ | --------------------------------- | ---- | ------------------------------------------------------------ | 2997| params | [GetDataParams](#getdataparams15) | Yes | Parameters required when an application uses the file copy capability provided by the pasteboard, including the destination path, file conflict option, and progress bar type.| 2998 2999**Return value** 3000 3001| Type | Description | 3002| -------------------------------------- | --------------------------------- | 3003| Promise<[PasteData](#pastedata)> | Promise used to return the system pasteboard data.| 3004 3005**Error codes** 3006 3007For details about the error codes, see [Pasteboard Error Codes](errorcode-pasteboard.md). 3008 3009| Error Code ID| Error Message | 3010| -------- | ------------------------------------------------------------ | 3011| 201 | Permission verification failed. The application does not have the permission required to call the API. | 3012| 401 | Parameter error. | 3013| 12900003 | Another copy or paste operation is in progress. | 3014| 12900007 | Invalid destUri or file system error. | 3015| 12900008 | Failed to start progress. | 3016| 12900009 | Progress exits abnormally. | 3017| 12900010 | System error occurred during paste execution. | 3018 3019**Example** 3020 3021```ts 3022import { BusinessError, pasteboard } from '@kit.BasicServicesKit'; 3023import { fileUri} from '@kit.CoreFileKit'; 3024@Entry 3025@Component 3026struct PasteboardTest { 3027 build() { 3028 RelativeContainer() { 3029 Column() { 3030 Column() { 3031 Button("Copy txt") 3032 .onClick(async ()=>{ 3033 let text = "test"; 3034 let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text); 3035 let systemPasteboard = pasteboard.getSystemPasteboard(); 3036 await systemPasteboard.setData(pasteData); 3037 let progressListenerInfo = (progress: pasteboard.ProgressInfo) => { 3038 console.info('progressListener success, progress:' + progress.progress); 3039 }; 3040 let destPath: string = '/data/storage/el2/base/files/'; 3041 let destUri : string = fileUri.getUriFromPath(destPath); 3042 let params: pasteboard.GetDataParams = { 3043 destUri: destUri, 3044 fileConflictOptions: pasteboard.FileConflictOptions.OVERWRITE, 3045 progressIndicator: pasteboard.ProgressIndicator.DEFAULT, 3046 progressListener: progressListenerInfo, 3047 }; 3048 systemPasteboard.getDataWithProgress(params).then((pasteData: pasteboard.PasteData) => { 3049 console.error('getDataWithProgress succ'); 3050 }).catch((err: BusinessError) => { 3051 console.error('Failed to get PasteData. Cause: ' + err.message); 3052 }) 3053 }) 3054 } 3055 } 3056 } 3057 } 3058} 3059``` 3060 3061### getChangeCount<sup>18+</sup> 3062 3063getChangeCount(): number 3064 3065Obtains the number of pasteboard content changes. 3066 3067Returns the number of pasteboard content changes if this API is called successfully; returns **0** otherwise. 3068 3069Even though the pasteboard data expires, or the data becomes empty because of the called [clearDataSync](#cleardatasync11) API, the number of data changes remains. 3070 3071When the system is restarted, or the pasteboard service is restarted due to an exception, the number of pasteboard data changes counts from 0. In addition, copying the same data repeatedly is considered to change the data for multiple times. Therefore, each time the data is copied, the number of data changes increases. 3072 3073**Atomic service API**: This API can be used in atomic services since API version 18. 3074 3075**System capability**: SystemCapability.MiscServices.Pasteboard 3076 3077**Return value** 3078 3079| Type| Description| 3080| -------- | -------- | 3081| number | The number of pasteboard content changes obtained.| 3082 3083**Example** 3084 3085```ts 3086import { BusinessError, pasteboard } from '@kit.BasicServicesKit'; 3087 3088let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 3089try { 3090 let result : number = systemPasteboard.getChangeCount(); 3091 console.info(`Succeeded in getting the ChangeCount. Result: ${result}`); 3092} catch (err) { 3093 console.error(`Failed to get the ChangeCount. Cause: ${err.message}`); 3094}; 3095``` 3096