1# Pasteboard 2 3The **pasteboard** module provides the copy and paste support for the system pasteboard. You can use the APIs of this module to operate pasteboard content of the plain text, HTML, URI, Want, pixel map, and other types. 4 5> **NOTE** 6> 7> 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. 8 9## Modules to Import 10 11```js 12import pasteboard from '@ohos.pasteboard'; 13``` 14 15## Attributes 16 17**System capability**: SystemCapability.MiscServices.Pasteboard 18 19| Name| Type| Readable| Writable| Description| 20| -------- | -------- | -------- | -------- | -------- | 21| MAX_RECORD_NUM<sup>7+</sup> | number | Yes| No| Maximum number of records in a **PasteData** object.| 22| MIMETYPE_TEXT_HTML<sup>7+</sup> | string | Yes| No| MIME type of the HTML content.| 23| MIMETYPE_TEXT_WANT<sup>7+</sup> | string | Yes| No| MIME type of the Want content.| 24| MIMETYPE_TEXT_PLAIN<sup>7+</sup> | string | Yes| No| MIME type of the plain text content.| 25| MIMETYPE_TEXT_URI<sup>7+</sup> | string | Yes| No| MIME type of the URI content.| 26 27 28## pasteboard.createPlainTextData 29 30createPlainTextData(text: string): PasteData 31 32Creates a **PasteData** object of the plain text type. 33 34**System capability**: SystemCapability.MiscServices.Pasteboard 35 36**Parameters** 37 38| Name| Type| Mandatory| Description| 39| -------- | -------- | -------- | -------- | 40| text | string | Yes| Plain text.| 41 42**Return value** 43 44| Type| Description| 45| -------- | -------- | 46| [PasteData](#pastedata) | **PasteData** object.| 47 48**Example** 49 50```js 51var pasteData = pasteboard.createPlainTextData("content"); 52``` 53 54 55## pasteboard.createHtmlData<sup>7+</sup> 56 57createHtmlData(htmlText: string): PasteData 58 59Creates a **PasteData** object of the HTML type. 60 61**System capability**: SystemCapability.MiscServices.Pasteboard 62 63**Parameters** 64 65| Name| Type| Mandatory| Description| 66| -------- | -------- | -------- | -------- | 67| htmlText | string | Yes| HTML content.| 68 69**Return value** 70 71| Type| Description| 72| -------- | -------- | 73| [PasteData](#pastedata) | **PasteData** object.| 74 75**Example** 76 77```js 78var 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>"; 79var pasteData = pasteboard.createHtmlData(html); 80``` 81 82 83## pasteboard.createWantData<sup>7+</sup> 84 85createWantData(want: Want): PasteData 86 87Creates a **PasteData** object of the Want type. 88 89**System capability**: SystemCapability.MiscServices.Pasteboard 90 91**Parameters** 92 93| Name| Type| Mandatory| Description| 94| -------- | -------- | -------- | -------- | 95| want | [Want](js-apis-featureAbility.md#want) | Yes| Want content.| 96 97**Return value** 98 99| Type| Description| 100| -------- | -------- | 101| [PasteData](#pastedata) | **PasteData** object.| 102 103**Example** 104 105```js 106var object = { 107 bundleName: "com.example.aafwk.test", 108 abilityName: "com.example.aafwk.test.TwoAbility" 109}; 110var pasteData = pasteboard.createWantData(object); 111``` 112 113 114## pasteboard.createUriData<sup>7+</sup> 115 116createUriData(uri: string): PasteData 117 118Creates a **PasteData** object of the URI type. 119 120**System capability**: SystemCapability.MiscServices.Pasteboard 121 122**Parameters** 123 124| Name| Type| Mandatory| Description| 125| -------- | -------- | -------- | -------- | 126| uri | string | Yes| URI content.| 127 128**Return value** 129 130| Type| Description| 131| -------- | -------- | 132| [PasteData](#pastedata) | **PasteData** object.| 133 134**Example** 135 136```js 137var pasteData = pasteboard.createUriData("dataability:///com.example.myapplication1/user.txt"); 138``` 139 140## pasteboard.createPlainTextRecord<sup>7+</sup> 141 142createPlainTextRecord(text: string): PasteDataRecord 143 144Creates a **PasteDataRecord** object of the plain text type. 145 146**System capability**: SystemCapability.MiscServices.Pasteboard 147 148**Parameters** 149 150| Name| Type| Mandatory| Description| 151| -------- | -------- | -------- | -------- | 152| text | string | Yes| Plain text.| 153 154**Return value** 155 156| Type| Description| 157| -------- | -------- | 158| [PasteDataRecord](#pastedatarecord7) | New **PasteDataRecord** object of the plain text type.| 159 160**Example** 161 162```js 163var record = pasteboard.createPlainTextRecord("hello"); 164``` 165 166 167## pasteboard.createHtmlTextRecord<sup>7+</sup> 168 169createHtmlTextRecord(htmlText: string): PasteDataRecord 170 171Creates a **PasteDataRecord** object of the HTML text type. 172 173**System capability**: SystemCapability.MiscServices.Pasteboard 174 175**Parameters** 176 177| Name| Type| Mandatory| Description| 178| -------- | -------- | -------- | -------- | 179| htmlText | string | Yes| HTML content.| 180 181**Return value** 182 183| Type| Description| 184| -------- | -------- | 185| [PasteDataRecord](#pastedatarecord7) | **PasteDataRecord** object of the HTML text type.| 186 187**Example** 188 189```js 190var 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>"; 191var record = pasteboard.createHtmlTextRecord(html); 192``` 193 194 195## pasteboard.createWantRecord<sup>7+</sup> 196 197createWantRecord(want: Want): PasteDataRecord 198 199Creates a **PasteDataRecord** object of the Want type. 200 201**System capability**: SystemCapability.MiscServices.Pasteboard 202 203**Parameters** 204 205| Name| Type| Mandatory| Description| 206| -------- | -------- | -------- | -------- | 207| want | [Want](js-apis-featureAbility.md#want) | Yes| Want content.| 208 209**Return value** 210 211| Type| Description| 212| -------- | -------- | 213| [PasteDataRecord](#pastedatarecord7) | New **PasteDataRecord** object of the Want type.| 214 215**Example** 216 217```js 218var object = { 219 bundleName: "com.example.aafwk.test", 220 abilityName: "com.example.aafwk.test.TwoAbility" 221}; 222var record = pasteboard.createWantRecord(object); 223``` 224 225 226## pasteboard.createUriRecord<sup>7+</sup> 227 228createUriRecord(uri: string): PasteDataRecord 229 230Creates a **PasteDataRecord** object of the URI type. 231 232**System capability**: SystemCapability.MiscServices.Pasteboard 233 234**Parameters** 235 236| Name| Type| Mandatory| Description| 237| -------- | -------- | -------- | -------- | 238| uri | string | Yes| URI content.| 239 240**Return value** 241 242| Type| Description| 243| -------- | -------- | 244| [PasteDataRecord](#pastedatarecord7) | New **PasteDataRecord** object of the URI type.| 245 246**Example** 247 248```js 249var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt"); 250``` 251 252## pasteboard.getSystemPasteboard 253 254getSystemPasteboard(): SystemPasteboard 255 256Obtains this **SystemPasteboard** object. 257 258**System capability**: SystemCapability.MiscServices.Pasteboard 259 260**Return value** 261 262| Type| Description| 263| -------- | -------- | 264| [SystemPasteboard](#systempasteboard) | **SystemPasteboard** object.| 265 266**Example** 267 268```js 269var systemPasteboard = pasteboard.getSystemPasteboard(); 270``` 271 272 273## PasteDataProperty<sup>7+</sup> 274 275Defines the properties of all data records on the pasteboard, including the timestamp, data type, and additional data. 276 277**System capability**: SystemCapability.MiscServices.Pasteboard 278 279| Name| Type| Readable| Writable| Description| 280| -------- | -------- | -------- | -------- | -------- | 281| additions | {[key:string]:object} | Yes| Yes| Additional data.| 282| mimeTypes | Array<string> | Yes| No| Non-repeating data types of the data records on the pasteboard.| 283| tag | string | Yes| Yes| Custom tag.| 284| timestamp | number | Yes| No| Timestamp when data is written to the pasteboard (unit: ms).| 285| localOnly | boolean | Yes| Yes| Whether the pasteboard content is set for local access only. The default value is **true**.<br>- **true**: The pasteboard content is set for local access only.<br>- **false**: The pasteboard content can be shared between devices.| 286 287 288## PasteDataRecord<sup>7+</sup> 289 290Provides **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. 291 292 293### Attributes 294 295**System capability**: SystemCapability.MiscServices.Pasteboard 296 297| Name| Type| Readable| Writable| Description| 298| -------- | -------- | -------- | -------- | -------- | 299| htmlText<sup>7+</sup> | string | Yes| No| HTML content.| 300| want<sup>7+</sup> | [Want](js-apis-featureAbility.md#want) | Yes| No| Want content.| 301| mimeType<sup>7+</sup> | string | Yes| No| Data type.| 302| plainText<sup>7+</sup> | string | Yes| No| Plain text.| 303| uri<sup>7+</sup> | string | Yes| No| URI content.| 304 305 306### convertToText<sup>7+</sup> 307 308convertToText(): Promise<string> 309 310Forcibly converts the content in a **PasteData** object to text. This API uses a promise to return the result. 311 312**System capability**: SystemCapability.MiscServices.Pasteboard 313 314**Return value** 315 316| Type| Description| 317| -------- | -------- | 318| Promise<string> | Promise used to return the text obtained from the conversion.| 319 320**Example** 321 322```js 323var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt"); 324record.convertToText().then((data) => { 325 console.info('Succeeded in converting to text. Data: ' + JSON.stringify(data)); 326}).catch((err) => { 327 console.error('Failed to convert to text. Cause: ' + JSON.stringify(err)); 328}); 329``` 330 331 332### convertToText<sup>7+</sup> 333 334convertToText(callback: AsyncCallback<string>): void 335 336Forcibly converts the content in a **PasteData** object to text. This API uses an asynchronous callback to return the result. 337 338**System capability**: SystemCapability.MiscServices.Pasteboard 339 340**Parameters** 341 342| Name| Type| Mandatory| Description| 343| -------- | -------- | -------- | -------- | 344| 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.| 345 346**Example** 347 348```js 349var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt"); 350record.convertToText((err, data) => { 351 if (err) { 352 console.error('Failed to convert to text. Cause: ' + JSON.stringify(err)); 353 return; 354 } 355 console.info('Succeeded in converting to text. Data: ' + JSON.stringify(data)); 356}); 357``` 358 359 360## PasteData 361 362Provides **PasteData** APIs. 363 364Before calling any **PasteData** API, you must obtain a **PasteData** object. 365 366**System capability**: SystemCapability.MiscServices.Pasteboard 367 368 369### getPrimaryText 370 371 372getPrimaryText(): string 373 374 375Obtains the plain text of the primary record. 376 377**System capability**: SystemCapability.MiscServices.Pasteboard 378 379 380**Return value** 381 382| Type| Description| 383| -------- | -------- | 384| string | Plain text.| 385 386**Example** 387 388```js 389var pasteData = pasteboard.createPlainTextData("hello"); 390var plainText = pasteData.getPrimaryText(); 391``` 392 393 394### getPrimaryHtml<sup>7+</sup> 395 396getPrimaryHtml(): string 397 398Obtains the HTML content of the primary record. 399 400**System capability**: SystemCapability.MiscServices.Pasteboard 401 402**Return value** 403 404| Type| Description| 405| -------- | -------- | 406| string | HTML content.| 407 408**Example** 409 410```js 411var 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>"; 412var pasteData = pasteboard.createHtmlData(html); 413var htmlText = pasteData.getPrimaryHtml(); 414``` 415 416 417### getPrimaryWant<sup>7+</sup> 418 419getPrimaryWant(): Want 420 421Obtains the Want object of the primary record. 422 423**System capability**: SystemCapability.MiscServices.Pasteboard 424 425**Return value** 426 427| Type| Description| 428| -------- | -------- | 429| [Want](js-apis-featureAbility.md#want) | Want object.| 430 431**Example** 432 433```js 434var object = { 435 bundleName: "com.example.aafwk.test", 436 abilityName: "com.example.aafwk.test.TwoAbility" 437}; 438var pasteData = pasteboard.createWantData(object); 439var want = pasteData.getPrimaryWant(); 440``` 441 442 443### getPrimaryUri<sup>7+</sup> 444 445getPrimaryUri(): string 446 447Obtains the URI of the primary record. 448 449**System capability**: SystemCapability.MiscServices.Pasteboard 450 451**Return value** 452 453| Type| Description| 454| -------- | -------- | 455| string | URI content.| 456 457**Example** 458 459```js 460var pasteData = pasteboard.createUriData("dataability:///com.example.myapplication1/user.txt"); 461var uri = pasteData.getPrimaryUri(); 462``` 463 464 465### addTextRecord<sup>7+</sup> 466 467addTextRecord(text: string): void 468 469Adds 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. 470 471The pasteboard supports a maximum number of 128 data records. 472 473**System capability**: SystemCapability.MiscServices.Pasteboard 474 475**Parameters** 476 477| Name| Type| Mandatory| Description| 478| -------- | -------- | -------- | -------- | 479| text | string | Yes| Plain text.| 480 481**Example** 482 483```js 484var pasteData = pasteboard.createPlainTextData("hello"); 485pasteData.addTextRecord("good"); 486``` 487 488 489### addHtmlRecord<sup>7+</sup> 490 491addHtmlRecord(htmlText: string): void 492 493Adds 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. 494 495The pasteboard supports a maximum number of 128 data records. 496 497**System capability**: SystemCapability.MiscServices.Pasteboard 498 499**Parameters** 500 501| Name| Type| Mandatory| Description| 502| -------- | -------- | -------- | -------- | 503| htmlText | string | Yes| HTML content.| 504 505**Example** 506 507```js 508var pasteData = pasteboard.createPlainTextData("hello"); 509var 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>"; 510pasteData.addHtmlRecord(html); 511``` 512 513 514### addWantRecord<sup>7+</sup> 515 516addWantRecord(want: Want): void 517 518Adds 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. 519 520The pasteboard supports a maximum number of 128 data records. 521 522**System capability**: SystemCapability.MiscServices.Pasteboard 523 524**Parameters** 525 526| Name| Type| Mandatory| Description| 527| -------- | -------- | -------- | -------- | 528| want | [Want](js-apis-featureAbility.md#want) | Yes| Want object.| 529 530**Example** 531 532```js 533var pasteData = pasteboard.createPlainTextData("hello"); 534var object = { 535 bundleName: "com.example.aafwk.test", 536 abilityName: "com.example.aafwk.test.TwoAbility" 537}; 538pasteData.addWantRecord(object); 539``` 540 541 542### addUriRecord<sup>7+</sup> 543 544addUriRecord(uri: string): void 545 546Adds 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. 547 548The pasteboard supports a maximum number of 128 data records. 549 550**System capability**: SystemCapability.MiscServices.Pasteboard 551 552**Parameters** 553 554| Name| Type| Mandatory| Description| 555| -------- | -------- | -------- | -------- | 556| uri | string | Yes| URI content.| 557 558**Example** 559 560```js 561var pasteData = pasteboard.createPlainTextData("hello"); 562pasteData.addUriRecord("dataability:///com.example.myapplication1/user.txt"); 563``` 564 565 566### addRecord<sup>7+</sup> 567 568addRecord(record: PasteDataRecord): void 569 570Adds a data record to this pasteboard, and adds its type to **mimeTypes** in [PasteDataProperty](#pastedataproperty7). The parameters cannot be empty. Otherwise, the operation fails. 571 572The pasteboard supports a maximum number of 128 data records. 573 574**System capability**: SystemCapability.MiscServices.Pasteboard 575 576**Parameters** 577 578| Name| Type| Mandatory| Description| 579| -------- | -------- | -------- | -------- | 580| record | [PasteDataRecord](#pastedatarecord7) | Yes| Record to add.| 581 582**Example** 583 584```js 585var pasteData = pasteboard.createUriData("dataability:///com.example.myapplication1/user.txt"); 586var textRecord = pasteboard.createPlainTextRecord("hello"); 587var 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>"; 588var htmlRecord = pasteboard.createHtmlTextRecord(html); 589pasteData.addRecord(textRecord); 590pasteData.addRecord(htmlRecord); 591``` 592 593 594### getMimeTypes<sup>7+</sup> 595 596getMimeTypes(): Array<string> 597 598Obtains a list of **mimeTypes** objects in [PasteDataProperty](#pastedataproperty7) from this pasteboard. If the pasteboard is empty, the returned list is also empty. 599 600**System capability**: SystemCapability.MiscServices.Pasteboard 601 602**Return value** 603 604| Type| Description| 605| -------- | -------- | 606| Array<string> | Non-repeating data types of the data records on the pasteboard.| 607 608**Example** 609 610```js 611var pasteData = pasteboard.createPlainTextData("hello"); 612var types = pasteData.getMimeTypes(); 613``` 614 615 616### getPrimaryMimeType<sup>7+</sup> 617 618getPrimaryMimeType(): string 619 620Obtains the data type of the primary record in this pasteboard. 621 622**System capability**: SystemCapability.MiscServices.Pasteboard 623 624**Return value** 625 626| Type| Description| 627| -------- | -------- | 628| string | Data type of the primary record.| 629 630**Example** 631 632```js 633var pasteData = pasteboard.createPlainTextData("hello"); 634var type = pasteData.getPrimaryMimeType(); 635``` 636 637 638### getProperty<sup>7+</sup> 639 640getProperty(): PasteDataProperty 641 642Obtains the property of the pasteboard data. 643 644**System capability**: SystemCapability.MiscServices.Pasteboard 645 646**Return value** 647 648| Type| Description| 649| -------- | -------- | 650| [PasteDataProperty](#pastedataproperty7) | Property of the pasteboard data.| 651 652**Example** 653 654```js 655var pasteData = pasteboard.createPlainTextData("hello"); 656var property = pasteData.getProperty(); 657``` 658 659### getRecordAt<sup>7+</sup> 660 661getRecordAt(index: number): PasteDataRecord 662 663Obtains the specified record in the pasteboard. 664 665**System capability**: SystemCapability.MiscServices.Pasteboard 666 667**Parameters** 668 669| Name| Type| Mandatory| Description| 670| -------- | -------- | -------- | -------- | 671| index | number | Yes| Index of the target record.| 672 673**Return value** 674 675| Type| Description| 676| -------- | -------- | 677| [PasteDataRecord](#pastedatarecord7) | Record with the specified index.| 678 679**Example** 680 681```js 682var pasteData = pasteboard.createPlainTextData("hello"); 683var record = pasteData.getRecordAt(0); 684``` 685 686 687### getRecordCount<sup>7+</sup> 688 689getRecordCount(): number 690 691Obtains the number of records in the pasteboard. 692 693**System capability**: SystemCapability.MiscServices.Pasteboard 694 695**Return value** 696 697| Type| Description| 698| -------- | -------- | 699| number | Number of records.| 700 701**Example** 702 703```js 704var pasteData = pasteboard.createPlainTextData("hello"); 705var count = pasteData.getRecordCount(); 706``` 707 708 709### getTag<sup>7+</sup> 710 711getTag(): string 712 713Obtains the custom tag from the pasteboard. If no custom tag is set, null is returned. 714 715**System capability**: SystemCapability.MiscServices.Pasteboard 716 717**Return value** 718 719| Type| Description| 720| -------- | -------- | 721| string | Custom tag. If no custom tag is set, null is returned.| 722 723**Example** 724 725```js 726var pasteData = pasteboard.createPlainTextData("hello"); 727var tag = pasteData.getTag(); 728``` 729 730 731### hasMimeType<sup>7+</sup> 732 733hasMimeType(mimeType: string): boolean 734 735Checks whether the pasteboard contains data of the specified type. 736 737**System capability**: SystemCapability.MiscServices.Pasteboard 738 739**Parameters** 740 741| Name| Type| Mandatory| Description| 742| -------- | -------- | -------- | -------- | 743| mimeType | string | Yes| Type of the data to query.| 744 745**Return value** 746 747| Type| Description| 748| -------- | -------- | 749| boolean | Returns **true** if the specified data type exists; returns **false** otherwise.| 750 751**Example** 752 753```js 754var pasteData = pasteboard.createPlainTextData("hello"); 755var hasType = pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_PLAIN); 756``` 757 758 759### removeRecordAt<sup>7+</sup> 760 761removeRecordAt(index: number): boolean 762 763Removes the record with the specified index from the pasteboard. 764 765**System capability**: SystemCapability.MiscServices.Pasteboard 766 767**Parameters** 768 769| Name| Type| Mandatory| Description| 770| -------- | -------- | -------- | -------- | 771| index | number | Yes| Specified index.| 772 773**Return value** 774 775| Type| Description| 776| -------- | -------- | 777| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 778 779**Example** 780 781```js 782var pasteData = pasteboard.createPlainTextData("hello"); 783var isRemove = pasteData.removeRecordAt(0); 784``` 785 786 787### replaceRecordAt<sup>7+</sup> 788 789replaceRecordAt(index: number, record: PasteDataRecord): boolean 790 791Replaces the record with the specified index in the pasteboard with a new record. 792 793**System capability**: SystemCapability.MiscServices.Pasteboard 794 795**Parameters** 796 797| Name| Type| Mandatory| Description| 798| -------- | -------- | -------- | -------- | 799| index | number | Yes| Specified index.| 800| record | [PasteDataRecord](#pastedatarecord7) | Yes| New record.| 801 802**Return value** 803 804| Type| Description| 805| -------- | -------- | 806| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 807 808**Example** 809 810```js 811var pasteData = pasteboard.createPlainTextData("hello"); 812var record = pasteboard.createUriRecord("dataability:///com.example.myapplication1/user.txt"); 813var isReplace = pasteData.replaceRecordAt(0, record); 814``` 815 816## SystemPasteboard 817 818Provides **SystemPasteboard** APIs. 819 820Before calling any **SystemPasteboard** API, you must obtain a **SystemPasteboard** object using [getSystemPasteboard](#pasteboardgetsystempasteboard). 821 822```js 823var systemPasteboard = pasteboard.getSystemPasteboard(); 824``` 825 826 827### setPasteData 828 829setPasteData(data: PasteData, callback: AsyncCallback<void>): void 830 831Writes a **PasteData** object to the pasteboard. This API uses an asynchronous callback to return the result. 832 833**System capability**: SystemCapability.MiscServices.Pasteboard 834 835**Parameters** 836 837| Name| Type| Mandatory| Description| 838| -------- | -------- | -------- | -------- | 839| data | [PasteData](#pastedata) | Yes| **PasteData** object.| 840| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 841 842**Example** 843 844```js 845var pasteData = pasteboard.createPlainTextData("content"); 846var systemPasteboard = pasteboard.getSystemPasteboard(); 847systemPasteboard.setPasteData(pasteData, (err, data) => { 848 if (err) { 849 console.error('Failed to set PasteData. Cause: ' + err.message); 850 return; 851 } 852 console.info('Succeeded in setting PasteData.'); 853}); 854``` 855 856 857### setPasteData 858 859setPasteData(data: PasteData): Promise<void> 860 861Writes a **PasteData** object to the pasteboard. This API uses a promise to return the result. 862 863**System capability**: SystemCapability.MiscServices.Pasteboard 864 865**Parameters** 866 867| Name| Type| Description| 868| -------- | -------- | -------- | 869| data | [PasteData](#pastedata) | **PasteData** object.| 870 871**Return value** 872 873| Type| Description| 874| -------- | -------- | 875| Promise<void> | Promise that returns no value.| 876 877**Example** 878 879```js 880var pasteData = pasteboard.createPlainTextData("content"); 881var systemPasteboard = pasteboard.getSystemPasteboard(); 882systemPasteboard.setPasteData(pasteData).then((data) => { 883 console.info('Succeeded in setting PasteData.'); 884}).catch((err) => { 885 console.error('Failed to set PasteData. Cause: ' + err.message); 886}); 887``` 888 889 890### getPasteData 891 892getPasteData( callback: AsyncCallback<PasteData>): void 893 894Obtains a **PasteData** object from the pasteboard. This API uses an asynchronous callback to return the result. 895 896**System capability**: SystemCapability.MiscServices.Pasteboard 897 898**Parameters** 899 900| Name| Type| Mandatory| Description| 901| -------- | -------- | -------- | -------- | 902| 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.| 903 904**Example** 905 906```js 907var systemPasteboard = pasteboard.getSystemPasteboard(); 908systemPasteboard.getPasteData((err, pasteData) => { 909 if (err) { 910 console.error('Failed to get PasteData. Cause: ' + err.message); 911 return; 912 } 913 var text = pasteData.getPrimaryText(); 914}); 915``` 916 917 918### getPasteData 919 920getPasteData(): Promise<PasteData> 921 922Obtains a **PasteData** object from the pasteboard. This API uses a promise to return the result. 923 924**System capability**: SystemCapability.MiscServices.Pasteboard 925 926**Return value** 927 928| Type| Description| 929| -------- | -------- | 930| Promise<[PasteData](#pastedata)> | Promise used to return the system pasteboard data.| 931 932**Example** 933 934```js 935var systemPasteboard = pasteboard.getSystemPasteboard(); 936systemPasteboard.getPasteData().then((pasteData) => { 937 var text = pasteData.getPrimaryText(); 938}).catch((err) => { 939 console.error('Failed to get PasteData. Cause: ' + err.message); 940}) 941``` 942 943 944### on('update')<sup>7+</sup> 945 946on(type: 'update', callback: () =>void ): void 947 948Subscribes to the content change event of the system pasteboard. 949 950**System capability**: SystemCapability.MiscServices.Pasteboard 951 952**Parameters** 953 954| Name| Type| Mandatory| Description| 955| -------- | -------- | -------- | -------- | 956| type | string | Yes| Event type. The value **'update'** indicates changes in the pasteboard content.| 957| callback | function | Yes| Callback invoked when the pasteboard content changes.| 958 959**Example** 960 961```js 962var systemPasteboard = pasteboard.getSystemPasteboard(); 963var listener = () => { 964 console.info('The system pasteboard has changed.'); 965}; 966systemPasteboard.on('update', listener); 967``` 968 969 970### off('update')<sup>7+</sup> 971 972off(type: 'update', callback?: () =>void ): void 973 974Unsubscribes from the system pasteboard content change event. 975 976**System capability**: SystemCapability.MiscServices.Pasteboard 977 978**Parameters** 979 980| Name| Type| Mandatory| Description| 981| -------- | -------- | -------- | -------- | 982| type | string | Yes| Event type. The value **'update'** indicates changes in the pasteboard content.| 983| callback | function | No| Callback invoked when the pasteboard content changes.| 984 985**Example** 986 987```js 988let listener = () => { 989 console.info('The system pasteboard has changed.'); 990}; 991systemPasteboard.off('update', listener); 992``` 993 994 995### hasPasteData<sup>7+</sup> 996 997hasPasteData(callback: AsyncCallback<boolean>): void 998 999Checks whether the system pasteboard contains data. This API uses an asynchronous callback to return the result. 1000 1001**System capability**: SystemCapability.MiscServices.Pasteboard 1002 1003**Parameters** 1004 1005| Name| Type| Mandatory| Description| 1006| -------- | -------- | -------- | -------- | 1007| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 1008 1009**Example** 1010 1011```js 1012systemPasteboard.hasPasteData((err, data) => { 1013 if (err) { 1014 console.error('Failed to check the PasteData. Cause: ' + JSON.stringify(err)); 1015 return; 1016 } 1017 console.info('Succeeded in checking the PasteData. Data: ' + JSON.stringify(data)); 1018}); 1019``` 1020 1021 1022### hasPasteData<sup>7+</sup> 1023 1024hasPasteData(): Promise<boolean> 1025 1026Checks whether the system pasteboard contains data. This API uses a promise to return the result. 1027 1028**System capability**: SystemCapability.MiscServices.Pasteboard 1029 1030**Return value** 1031 1032| Type| Description| 1033| -------- | -------- | 1034| Promise<boolean> | Promise used to return the result. Returns **true** if the system pasteboard contains data; returns **false** otherwise.| 1035 1036**Example** 1037 1038```js 1039systemPasteboard.hasPasteData().then((data) => { 1040 console.info('Succeeded in checking the PasteData. Data: ' + JSON.stringify(data)); 1041}).catch((err) => { 1042 console.error('Failed to check the PasteData. Cause: ' + JSON.stringify(err)); 1043}); 1044``` 1045 1046 1047### clear<sup>7+</sup> 1048 1049clear(callback: AsyncCallback<void>): void 1050 1051Clears the system pasteboard. This API uses an asynchronous callback to return the result. 1052 1053**System capability**: SystemCapability.MiscServices.Pasteboard 1054 1055**Parameters** 1056 1057| Name| Type| Mandatory| Description| 1058| -------- | -------- | -------- | -------- | 1059| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1060 1061**Example** 1062 1063```js 1064systemPasteboard.clear((err, data) => { 1065 if (err) { 1066 console.error('Failed to clear the PasteData. Cause: ' + JSON.stringify(err)); 1067 return; 1068 } 1069 console.info('Succeeded in clearing the PasteData.'); 1070}); 1071``` 1072 1073 1074### clear<sup>7+</sup> 1075 1076clear(): Promise<void> 1077 1078Clears the system pasteboard. This API uses a promise to return the result. 1079 1080**System capability**: SystemCapability.MiscServices.Pasteboard 1081 1082**Return value** 1083 1084| Type| Description| 1085| -------- | -------- | 1086| Promise<void> | Promise that returns no value.| 1087 1088**Example** 1089 1090```js 1091systemPasteboard.clear().then((data) => { 1092 console.info('Succeeded in clearing the PasteData.'); 1093}).catch((err) => { 1094 console.error('Failed to clear the PasteData. Cause: ' + JSON.stringify(err)); 1095}); 1096``` 1097